Thread: keywords

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    keywords

    Is there a way to use a keyword as a variable name? Well, you will answer who cares about the name, but I am just wondering....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In C++, keywords are reserved words that may not be used in any other context. On the other hand, names and keywords are case sensitive...
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    While laserlight is kind enough to point out that keywords are case sensitive, I think its a dubious practice to be writing code that untilizes that fact unless....

    Example:
    Code:
    class Int
    {
      // write a class that works generally like an int, but has some nifty member functions as well
    };
    I guess it all depends on what you are trying to do.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    No.

    Macros can be key words, so you can make it look like a keyword is a variable, by making it a macro to some other name. DO NOT DO THIS.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Macros can be key words, so you can make it look like a keyword is a variable, by making it a macro to some other name.
    That is true, since semantic analysis only happens after macro expansion. On the other hand, the program that results from macro expansion does not contain a keyword as a variable name, so the unqualified "no" is misleading, since it is still true that keywords are reserved words that may not be used in any other context.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    typedef int whatever;
    #define int whatever
    Behold! Ok... that isn't exactly an impressive usage of what King is talking about. The one interesting and useful aspect of doing this is when you are trying to default something to signed or unsigned.

    Example:
    Code:
    typedef unsigned int uint;
    
    #define int uint
    But even that is no substitute for a decent makefile since doing something similar to the above example will result in problems when you try to later use a signed int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to color certain keywords?
    By Purity in forum Tech Board
    Replies: 5
    Last Post: 10-20-2005, 02:07 PM
  2. super/interface keywords
    By xErath in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2005, 07:06 PM
  3. Google remembering my keywords
    By m712 in forum Tech Board
    Replies: 4
    Last Post: 07-26-2003, 11:48 AM
  4. new keywords
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 07-25-2002, 02:57 AM
  5. C# Keywords
    By Troll_King in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-28-2001, 11:00 AM