Friday, July 31, 2009

Is there something that will compile in C but not in C++?

it is a interview question

Is there something that will compile in C but not in C++?
From Page 26 of C++ Complete Reference Third Edition:





Because C/C++ allows separate modules of a large program to be separately compiled


and linked together, there must be some way of telling all the files about the global


variables required by the program. Although C technically allows you to define a


global variable more than once, it is not good practice (and may cause problems when


linking). More importantly, in C++, you may define a global variable only once.
Reply:No, Nothing in C doesn't work under C++.


Any C Code works 100% under C++.
Reply:No, C++ is completely compatible with C. Linking is another issue, though.
Reply:Sure, anything that has been changed in C++ from C will affect the code.





Case in point:


int p = malloc(sizeof p).





Standard malloc idiom in C. You normally use new in C++, but if you tried to do the above malloc in C++ you would get a compile error on incompatible types. You need an explicit cast in C++.

hibiscus

No comments:

Post a Comment