Calling enums in another class in C++

10,045

Solution 1

In C++, . and -> are for accessing a member of this particular instance of an A object. :: is for accessing things in the scope of class A. This includes statics, enums, and function pointers.

So in C++ you want A::F or A::G if you want enum value.

for the enum type you do A::E

class A
{
public:
    enum E { F , G , H};
    virtual ~A() = 0; //a pure virtual function to make the class abstract
};

int main()
{
    A::E x; // x is declared as the enum
    x = A::F; // x is assigned a particular enum value

    return 0;
}

Also to make the class abstract you provide a pure virtual function.

Solution 2

C# has single . operator. C++ has multiple: ., -> and ::. For classes you use the last one.

Therefore, it is going to be A::E.

Remember also, that in C++ the enum's constant go to parent namespace, not the one of enum like it is in C#. Therefore, in C# you would have A.E.F, but in C++ you have A::F.

Share:
10,045
derekhh
Author by

derekhh

Currently I am a software engineer at Snap Inc. Previously I've been working at Microsoft Bing for almost four years. During my years at Microsoft I was the primary back-end algorithm developer or tech lead for many features related to natural language processing and machine learning. I've designed and implemented many core algorithms that powered Bing's conversational experience, question answering and entity carousel. Prior to joining Microsoft, I've obtained my Ph.D. from the Hong Kong University of Science and Technology. My research was primarily on the theme of sensor-based human activity recognition. Throughout my PhD years, I've published around 20 papers in top conferences and journals. I was also a winner of the Microsoft Research Fellowship in the year 2009. I also enjoy competitive programming a lot. I was a regular contestant in programming contests like acm/icpc, Google Code Jam and TopCoder Open. I've also won awards and top prizes from these competitions. Google Scholar Page: https://scholar.google.com/citations?user=Ks81aO0AAAAJ&hl=en Specialties: machine learning, data mining, algorithms, programming

Updated on June 28, 2022

Comments

  • derekhh
    derekhh almost 2 years

    If we define two C++ classes. One is:

    abstract class A {
      public:
        enum E {F, G, H;}
    };
    

    Another is class B, and how I can use the enum E in class A then? Assuming both B and A are in the same namespace. I know in C# we can use something like:

    A.E
    

    directly, but seems that is not the case of C++.

  • Mooing Duck
    Mooing Duck over 12 years
    Added a concept that I thought was meaningful and missing from your otherwise good answer.
  • parapura rajkumar
    parapura rajkumar over 12 years
    @MD.. Thanks... I hope from my anwser it is clear that you don't do A::E::F which is common mistake
  • Mooing Duck
    Mooing Duck over 12 years
    As of C++11, that is now allowed. (And I've always done that anyway, since MSVC allowed it as an extension)
  • Raghav55
    Raghav55 over 12 years
    I was just curious to know why destructor was made pure virtual function?
  • Krizz
    Krizz over 12 years
    It is more of a question to the answer above. But I guess this is a way to prevent class from being instantiated. Never used that myself though.