473,692 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Override Enumeration in Derived Class

2 New Member
Is there any way of declaring an enumeration in a base class in such a way that it can be "overridden " in a derived class... for instance:

class Base{

public:
enum Enum { //empty };

}

class Derived : public Base{

enum Enum
{
A = 0,
B = 1,
C = 2
}

}

In short, I am trying to create an enumeration "object" that will enable an enumeration to be used in a more OO manner...

The way I am trying to implement this is by creating a base class with a base enumeration definition, a pure virtual init() that will (later in derived classes) be used to create a list of the form:

//for the enum above...

typedef std:pair<Base:: Enum, std::string> EnumPair;
std::list<Base: :EnumPair> EnumList;

init()
{
EnumList.push_b ack( EnumPair(A, "A") );
EnumList.push_b ack( EnumPair(B, "B") );
EnumList.push_b ack( EnumPair(C, "C") );
}

and a set of operators & methods that essential allow enum vs. string comparisons and translations...

I've been able to do this with a single enumeration however, since the majority of the code simply parses a list, I would like to reduce the overhead of creating future enumeration "objects" by creating a base class that defines the majority of the operators & methods need and which would allow derived classes to simply override the enumeration & create a virtual init() method that would initialize the inherited EnumList as in the example above...

I know that this is likely going to be troublesome since enumerations are considered as a "type" in C++ but I'm trying nonetheless (shoot the moon)...

Any advice would be much appreciated!
Aug 21 '07 #1
3 13675
RRick
463 Recognized Expert Contributor
As far as I can tell, enums are const ints with pretty names first found in C. Hmmm, they don't sound very OO to me.

I like your idea, but I would suggest using a map instead of a list. Your searches will be a lot faster. Also, I think the multiple definitions of Enum are going to confuse the compiler. How will it know when you're talking about Base::Enum instead of Derived::Enum? If you get rid of Derived::Enum, then you can force the map to return an enum type for any and all values stored in the map. For example,
Expand|Select|Wrap|Line Numbers
  1. EnumList AList;
  2. // Just need an integer value, can be enum or not
  3. Alist.push( A or 22 or ??, "A"); etc. etc.
  4. Enum gotA = AList( "A") ;  
  5.  
Unfortunately, now we can't have the compiler tell the difference between an AList and a BList. Equally bad is that this is a runtime cost, and not a compiler cost you get with enums.

You might want to take a look at the Java (!gasp) Enum class. They allow the class to be overridden. I tried it once and found it to be awkward but workable.
Aug 21 '07 #2
scotchfx
2 New Member
Java isn't really an option for me unfortunately.. .

I did use a map initially but I want to be able to search on both the enumeration and it's respective string... and as I am creating this for a user defined enumeration the lists won't be long enough to realize the logarithmic performance gain of a hash/map...

Would it be possible to template a class to do something similar to this instead of trying to use inheritance?
Aug 22 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
As far as I can tell, enums are const ints with pretty names first found in C. Hmmm, they don't sound very OO to me.
enums are not integers. They are not variables at all. An enum is a declaration.

Instead, an enum is a list of named integer values. If you define an integer with a type of the enum, it is still an int but the compiler can restrict the values to the named values in the enum declaration.

Having an enum declared within a class is recommended. One reason is that you can use the same enum name in different classes with different values. Also, it removes the requirement for a giant enum header that contains unique names a la C.

In terms of overriding, dio you try:
Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3. public:
  4.     enum Error {OUCH};
  5. };
  6. class Derived : public Base
  7. {
  8. public:
  9.     enum Error {OHBOY, OUCH};
  10. };
  11.  
  12. int main()
  13. {
  14.     Base::Error valB = Base::OUCH;
  15.     Derived::Error valA = Derived::OHBOY;
  16.     valA = Derived::OUCH;
  17. }
  18.  
Aug 22 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

24
3296
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
9
3674
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; template<class T> classSubject {
2
5036
by: Steve James | last post by:
I am trying to mark an override method in a derived class as obsolete using the ObsoleteAttribute. The compiler, however is not picking up this attribute and is not generating a warning or an error. Sample code is below: If I put the attribute on the static method DoSomething() in the main class, a warning/error is generated by the compiler. If I put the attribute on the virtual instance method DoSomething() of
9
2066
by: Ken Varn | last post by:
Is there anyway to override a public virtual method or property so that it is private in my derived class? I tried using new on the property and making it private, but no luck. -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc.
14
24589
by: Edward Diener | last post by:
In C++ one can change the access level in a derived class by the "using" keyword. So, for instance, a public method in a base class can be made private in a derived class. Is there any way of doing this in C# ?
14
7088
by: knocte | last post by:
Hello. I have a problem with C# language. I want to define an algorithm on a static function inside an abstract class. This function calls a static variable inside the same class. Then I want to define some derived classes wich inherit the function and override the variable, but it doesn't work. Testcase:
9
1797
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't figure out what attribute to put on the base class method to prevent this. TIA, Larry Woods
1
2191
by: archana | last post by:
Hi all, I am confuse in concept of inheritance. I am having following 2 classes class base1 { public void abc() { System.Console.WriteLine("base1 abc"); }
3
2222
by: Goran Djuranovic | last post by:
Hi all, Is there a way to retrieve a derived class name inside a subroutine or a function of the base class? I am trying to get some data from the database, based on which derived class is calling the subroutine. I know the base class name can be retrieved by using reflection namespace: System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name. Thanks Goran
0
8603
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9083
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8797
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8800
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6459
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5818
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4323
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2237
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.