Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How can i inside enum in class

How can i inside enum in class

Scheduled Pinned Locked Moved C++ Gurus
9 Posts 7 Posters 20.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ruzik
    wrote on last edited by
    #1

    Hellow, how can i inside enum in class?
    I wanna that i can called enum item throught class:
    @ClassName::EnumElement@
    If i declare enum outside class, for example
    @
    enum En{One, Second};
    class c{
    }@
    I could write En::One instead of с::One, but if i write
    @
    class c{
    enum En{One, Second};
    }@
    Compiler get me many errors
    Advance many thanks for your help!

    Edit: Moved to C++ Gurus as this is not Qt specific [ZapB]

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thisisbhaskar
      wrote on last edited by
      #2

      Can you paste the errors you are getting at compile time.. this should work as I see it...

      1 Reply Last reply
      0
      • T Offline
        T Offline
        task_struct
        wrote on last edited by
        #3

        enum En is private for class c. Where do you try to access values? Member functions or outside class?

        "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

        • Linu...
        1 Reply Last reply
        0
        • JohanSoloJ Offline
          JohanSoloJ Offline
          JohanSolo
          wrote on last edited by
          #4

          If it's written exactly as you showed it to us, the enum is private, meaning that you won't be able to do c::One outside of your class.

          If the enum is public, then post the real piece of code and the compilation errors as already asked

          `They did not know it was impossible, so they did it.'
          -- Mark Twain

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on last edited by
            #5

            This works for me:

            @class MyClass
            {
            public:
            enum MyEnum
            {
            Type_One,
            Type_Two
            };
            };@

            1 Reply Last reply
            0
            • T Offline
              T Offline
              thisisbhaskar
              wrote on last edited by
              #6

              [quote author="KA51O" date="1311579884"]This works for me:

              @class MyClass
              {
              public:
              enum MyEnum
              {
              Type_One,
              Type_Two
              };
              };@[/quote]

              Even the code posted by Ruzik should work provided that its used only inside the class..

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Ruzik
                wrote on last edited by
                #7

                Many thanks for your help, i found my mistake

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #8

                  Just for the records: enums in C++ do not use the enum name for accessing enum values.

                  @
                  class TestClass
                  {
                  public:
                  enum Enum
                  {
                  Value1,
                  Value2,
                  Value3
                  };
                  };
                  ...
                  TestClass::Enum enumVariable = TestClass::Enum::Value1; // not correct
                  TestClass::Enum enumVariable = TestClass::Value1; // correct
                  ...
                  @

                  This is why good class design usually requires that the enum name is part of the enum values (as also seen in Qt).

                  @
                  class TestClass
                  {
                  public:
                  enum Error
                  {
                  InternalError,
                  ExternalError
                  };
                  };
                  @

                  And keep in mind that class members - as already stated by others - are private by default.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    [quote author="Lukas Geyer" date="1311582311"]Just for the records: enums in C++ do not use the enum name for accessing enum values.
                    (...)
                    This is why good class design usually requires that the enum name is part of the enum values (as also seen in Qt).
                    [/quote]

                    Also note that this is about to "change":http://en.wikipedia.org/wiki/C++0x#Strongly_typed_enumerations in C++0x. There, the enum name does become part of the value name. You could change your second example to this then:

                    @
                    class TestClass
                    {
                    public:
                    class enum Error //note the 'class' in front of the enum keyword
                    {
                    Internal, //Note that you no longer need the Error postfix
                    External
                    };
                    };

                    ...

                    TestClass::Error errorVariable = TestClass::Error::Internal; // correct in C++0x
                    @

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved