inheriting enums

L

lou zion

hi all,

i'm trying to use a classes enums in another class, but can't seem to find
the right syntax.

i've got class A which has:

enum EditTypeA {Currency, Percent, LabeledNumber, Text};
EditTypeA LineType; // LineType holds widget type, and will be either
Currency, Percent, LabeledNumber or Text

and i have class B with no enums.

i'm defining class C which is not a subclass of either A or B, but uses
classes from both. however, class B is almost exactly like class A except it
has this B widget in it, so i want all of the functionality of A in C.

one of the functions in A is
EditTypeA A::GetLineType() const {};

i want C to have the exact same function with the same output type
EditTypeA, but the following produces a syntax error for relatively obvious
reasons: (all header files have been included)

A::EditTypeA C::GetLineType() const {};

how can i get C to inherit all the enums from A. unfortunately subclassing
is not an option in this case.

thanks! lou
 
V

Victor Bazarov

lou said:
i'm trying to use a classes enums in another class, but can't seem to find
the right syntax.

i've got class A which has:

Why write this when you can write

class A {
public:
enum EditTypeA {Currency, Percent, LabeledNumber, Text};
EditTypeA LineType; // LineType holds widget type, and will be either
Currency, Percent, LabeledNumber or Text

}; // class A
and i have class B with no enums.

i'm defining class C which is not a subclass of either A or B, but uses
classes from both. however, class B is almost exactly like class A except it
has this B widget in it, so i want all of the functionality of A in C.

one of the functions in A is
EditTypeA A::GetLineType() const {};

Is that defined _inside_ A or _outside? What's the semicolon doing after
the closing curly brace? If this function is supposed to return a value,
shouldn't it at least have a 'return' statement in its body?

Why don't you post _real_ code?
i want C to have the exact same function with the same output type
EditTypeA, but the following produces a syntax error for relatively obvious
reasons: (all header files have been included)

A::EditTypeA C::GetLineType() const {};

Again, where is this? Inside 'C' or outside? Why do the reasons seem so
"obvious" to you? And again, to return a value this function cannot have
an empty body.
how can i get C to inherit all the enums from A. unfortunately subclassing
is not an option in this case.

You cannot _inherit_ without _deriving_.

Post _real_ code.

V
 
T

Thomas Tutone

lou said:
hi all,

i'm trying to use a classes enums in another class, but can't seem to find
the right syntax.

i've got class A which has:

enum EditTypeA {Currency, Percent, LabeledNumber, Text};
EditTypeA LineType; // LineType holds widget type, and will be either
Currency, Percent, LabeledNumber or Text

and i have class B with no enums.

i'm defining class C which is not a subclass of either A or B, but uses
classes from both. however, class B is almost exactly like class A except it
has this B widget in it, so i want all of the functionality of A in C.

one of the functions in A is
EditTypeA A::GetLineType() const {};

i want C to have the exact same function with the same output type
EditTypeA, but the following produces a syntax error for relatively obvious
reasons: (all header files have been included)

A::EditTypeA C::GetLineType() const {};

The "relatively obvious reasons" are presumably (a) that you have
included a spurious semicolon at the end and (b) that this the function
omits a return statement, both of which have nothing to do with the
problem you are trying to solve.
how can i get C to inherit all the enums from A.

If they are public, C can just use them. You need to look at FAQ 5.8:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

In any case, the following compiles fine for me:

class A {
public:
enum EditTypeA {Currency, Percent, LabeledNumber, Text};
};

class C {
public:
A::EditTypeA GetLineType() const { return A::Currency; }
};

int main()
{
C c;
c.GetLineType();
}

What is it you are trying to do that is different from what appears
above?

Best regards,

Tom
 
B

Ben Pope

lou said:
hi all,

i'm trying to use a classes enums in another class, but can't seem to find
the right syntax.

i've got class A which has:

enum EditTypeA {Currency, Percent, LabeledNumber, Text};
EditTypeA LineType; // LineType holds widget type, and will be either
Currency, Percent, LabeledNumber or Text

Why can't you just show us class A? I'll start you off:

class A {
pubic:
EditTypeA {Currency, Percent, LabeledNumber, Text};
private:
EditTypeA LineType;
};
and i have class B with no enums.

Is class B relevant?
i'm defining class C which is not a subclass of either A or B,
Right.

but uses classes from both.

Uses how? Has a member of that type?

class C {
A a;
B b;
};

??
however, class B is almost exactly like class A except it
has this B widget in it, so i want all of the functionality of A in C.

Confused, is there a typo in there?
one of the functions in A is
EditTypeA A::GetLineType() const {};

class A {
pubic:
EditTypeA {Currency, Percent, LabeledNumber, Text};
EditTypeA A::GetLineType() const { return LineType; };
private:
EditTypeA LineType;
};

??
i want C to have the exact same function with the same output type
EditTypeA, but the following produces a syntax error for relatively obvious
reasons: (all header files have been included)

A::EditTypeA C::GetLineType() const {};

class C {
public:
A::EditTypeA GetLineType() const { return a.GetLineType(); };
private:
A a;
B b;
};

??
how can i get C to inherit all the enums from A. unfortunately subclassing
is not an option in this case.

I'm a bit confused. If the enum is not specific to A, why is it a
member of A? Just take it to a wider scope.

Can you post the code you're using? Your description is vague, the code
is much easier for us to work with.

Ben Pope
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top