470,858 Members | 1,663 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,858 developers. It's quick & easy.

Inheritance and enum


Does anyone have a suggestion how to solve this in a better way?

I have an abstract class A containing an std::vector<> of Mat (other class; not important here), with an enum specifying what sort of information the vector holds at each index.

class A {

enum matIndex {

goalA = 0,


std::vector<Mat> matrices;

Mat& getMat(matIndex i) { return matrices[i]; }
// lots of other functions
virtual void foo() = 0;


There are concrete classes inheriting from it, so the whole thing is pretty big.

Now I need another class B that's the same, except the enum is different (also different size). So I just copied A and inheriting classes and changed the names and enum, which works.

But now every time I change something, I have to change it in both places. Does anyone see a better method of doing this?

For example, if it was possible to define the enum at run-time in the constructor of the class (I know it isn't), my problem would be solved, just give an argument there.
Nov 29 '07 #1
1 3540
9,208 Expert Mod 8TB
Is it not possible to use the scope resolution operator:
Expand|Select|Wrap|Line Numbers
  1. if (data == B::NOPE || A::NOPE)
  2. etc...
That way A's enum could have the common values and B enum could have the additions and if the two conflicted (as above) you could just identify whose enum to use.
Nov 29 '07 #2

Post your reply

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

Similar topics

5 posts views Thread by Thomas Matthews | last post: by
2 posts views Thread by Alexander Lurye | last post: by
3 posts views Thread by Matteo Settenvini | last post: by
9 posts views Thread by Joe | last post: by
2 posts views Thread by Richard Myers | last post: by
1 post views Thread by FefeOxy | last post: by
1 post views Thread by Imre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.