How do you use enum delcared in a class?

This is a discussion on How do you use enum delcared in a class? within the C++ Programming forums, part of the General Programming Boards category; Hello, I have delcared a class with enum as follows: Code: class TokenPrint { protected: public: static enum TokenType {TOKEN_ANCHOR=0, ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    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..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    Posts
    22,915
    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, 11:14 AM
  3. How many times can time variables be delcared?
    By Qui in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2004, 05: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, 10:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21