开发者

What prevents C++ from being a strict superset of C? [duplicate]

开发者 https://www.devze.com 2023-01-17 19:17 出处:网络
This question already has answers here: Closed 12 years ago开发者_JAVA百科. Possible Duplicate: “C subset of C++” -> Where not ? examples ?
This question already has answers here: Closed 12 years ago开发者_JAVA百科.

Possible Duplicate:

“C subset of C++” -> Where not ? examples ?

I'm aware that C++ is not a strict superset of C. What language features prevent C++ from being a superset of C?


The elephant in the room: the following is valid C but not valid C++.

int typename = 1;

Substitute your favorite C++ reserved word.


C++ also does not support variable-length arrays, where:

int array[n];

is valid in C, but not C++. A C++ version of the above would be:

int *array = new int[n];
  ...
delete [] array;


There is a special wiki entry that summarizes a lot of issues.


Simple example, consider this declaration:

int f();

This is valid C, but invalid C++: f(3, 2, -5, "wtf");

Explanation: in C, int f() is treated like int f(...) (at least at the first call site). Declare as int f(void) if you don't want f to take parameters.


One from top of my head - C++ does not support default int.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号