Initialization of an enumeration through constructors

I have an enumeration as a public memebr class as follows:

class myClass
{
    public:
        enum myEnum
        {
            myEnum1,
            myEnum2
        };
};

I also declare public constructors parameterized as follows:

myClass(myEnum);

I define the same thing outside the class definition as follows:

myClass :: myClass(myEnum E)
{
}

How to initialize myEnum with default arguments?

I tried:

i)

myClass :: myClass(myEnum E = 0); //to int error

b)

myClass :: myClass(myEnum E = {0}); //some error

III)

myClass :: myClass(myEnum E = {0, 1}); //some error

One thing that I still do not understand.

How to pass enum values ​​to a constructor. I am still getting an error. And I need to do it like this:

derived(arg1, arg2, arg3, enumHere, arg4, arg5); //call

Function Definition:

derived(int a, int b, int c, enumHere, int 4, int 5) : base(a, b);

Where should I initialize the enumeration, as one of the answers below?

+5
source share
1 answer

, . myEnum , .

:

class myClass
{
    public:
        enum myEnum
        {
            myEnum1,
            myEnum2
        } enumField;
};

:

myClass::myClass() : enumField(myEnum1) {};

:

myClass::myClass(myEnum e) : enumField(e) {};

:

myClass(myEnum e = myEnum1) : enumField(e) {};

:

myDerived(myEnum e) : myClass(e) {};
+9

All Articles