473,320 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

inheriting enums

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

Feb 14 '06 #1
3 1635

lou zion wrote:
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-lit...t.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

Feb 14 '06 #2
lou zion wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 14 '06 #3
lou zion wrote:
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
--
Please remove capital As from my address when replying by mail
Feb 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
1
by: Dan Thompson | last post by:
I am developing a class that inherits the Event Log Class. I want to make the EventLogEntryType enum available as part of the same namespace as the class I am writing but I don't think I can inherit...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
2
by: SpotNet | last post by:
Hello CSharpies, Can Enums in C# be UInt32 Types, and not just Int32 Types. I have a lot of constant declarations that are UInt32 that I want to put in Enums to ease the use of Intellisence. I...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
2
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default,...
11
by: Marc Gravell | last post by:
This one stumped me while refactoring some code to use generics... Suppose I declare an enum MyEnum {...} Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ? Of course,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.