开发者

reserved keywords in C++

开发者 https://www.devze.com 2023-01-25 14:14 出处:网络
I think there are reserved keywords开发者_如何学JAVA in C++ , why they are called \'reserve keywords \' and categorized as different?Words used by the language (if, for, while, struct, etc.) are somet

I think there are reserved keywords开发者_如何学JAVA in C++ , why they are called 'reserve keywords ' and categorized as different?


Words used by the language (if, for, while, struct, etc.) are sometimes called reserved words or keywords. I suspect that many people also call them reserved keywords.

However, there are also words that the Committee may want to use in the future but that aren't currently used. I would consider these reserved keywords. In Java goto is a reserved word that isn't currently used (although I believe the idea is that it won't ever be used). This kind of reserved keyword isn't allowed as a variable name, function name, class, struct or enum name, etc. But without any use in the language today, it wouldn't really be allowed in valid C++ at all.


Reserved words or keywords are predefined words with special meanings in that language:
http://en.wikipedia.org/wiki/Reserved_word
http://en.wikipedia.org/wiki/Keyword_(computer_programming)


Keywords are names like "if" or "while" used in the syntax of the language. If they are reserved, they can't be used as identifiers. If they aren't reserved (contextual keywords) they can be used as identifiers, they are used as keywords only in specific contexts (where identifiers can't appear, or if they can, there is some rule which gives priority to the identifier or to the keyword function). C++0X will probably have contextual keywords. A language famous for its (ab)use contextual keywords was PL/I in which you could write something like:

IF IF THEN THEN = ELSE; ELSE ELSE = THEN;

(Obviously, the sane approach is to consider even the contextual keywords reserved; the utility is contextual keywords is helping migration).


And let's end with a C++ reserved keyword story: When I just began learning C++ I had the worst teacher.

One of his assignments was to write a "phone book". The design we were given was pretty simple,

you have a phone book class which stores an array of classes called: friend. Back in the days I studied C++ we didn't had internet.

So half of us had a compilation problem and the other half didn't. Our teacher just shrugged and said "No problem, half of you failed". It took us time to realize that the half that had the compilation problem were the half that the friend word was colored different in the IDE, the same color that the struct \ enum \ class keywords were colored. The other half called their class Friend (capital F).

0

精彩评论

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

关注公众号