Thread: How do you use enum delcared in a class?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    27

    How do you use enum delcared in a class?

    Hello,
    I have delcared a class with enum as follows:
    Code:
    class TokenPrint {
    protected:
    public:
        static enum TokenType {TOKEN_ANCHOR=0, TOKEN_ARGUMENT_STRING=1, 
    TOKEN_ARGUMENT_INT=2, TOKEN_VALUE_STRING=3, TOKEN_VALUE_INT=4} TokenType;
        enum TokenType tokenType ;
    tokenType is a enum variable that can take a value of 5 values list above.
    In my copy consturctor, the assignment didn't generate any compile error as long as I access it as a local variable.
    Code:
    TokenPrint::TokenPrint(const TokenPrint &_tokenPrint) {
    	tokenType=_tokenPrint.tokenType;
    	tokenType=TOKEN_ANCHOR; //compiles ok
            tokenType=TokenPrint.TOKEN_ANCHOR; //compile error
    }
    This creates a problem because I am trying to initialize the tokenType outside of the class functions:
    Code:
    tokenPrint.tokenType=TokenPrint.TokenType.TOKEN_ACHOR;  //compile error
    I tried to move the static enum TokenType delcaration outside of the class, but it gives me duplicate definition. I can probably find a work-around using #ifnef
    but it's just useful to keep all the declaration inside the class.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    but it's just useful to keep all the declaration inside the class.
    Why?
    If you think of the enum as a separate type, it isn't useful to put it inside the class at all..

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You seem to be thinking that an enum is somehow a member variable. It is not, so dot notation is completely useless here. You have to use TokenPrint:: to get inside the class namespace, just like you do for everything else inside a class.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is also no need for "static" here. Unlike Java, types do not belong to instances.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enum within a class
    By manasij7479 in forum C++ Programming
    Replies: 2
    Last Post: 02-06-2011, 12:33 PM
  2. Enum type In class.
    By epidemic in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2007, 10:14 AM
  3. How many times can time variables be delcared?
    By Qui in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2004, 04:24 PM
  4. enum in class
    By gipsy in forum C++ Programming
    Replies: 1
    Last Post: 12-28-2002, 07:16 AM
  5. friends class lil prblm with enum
    By rip1968 in forum C++ Programming
    Replies: 4
    Last Post: 07-25-2002, 09:57 PM