Internal Enum and Inheritance

F

FefeOxy

Hi,

This is a hypothetical thing that I want to test out, and if it works,
I will be using it to enhance my code.

// --- Suppose I have an abstract base class: ---

class A {
public:
A(void) { }
virtual A(void) { }

//...some virtual functions....
virtual void vFunc(int n)=0;
};

// --- and 2 class that inherits it.. .---

class B : public A {
public:
//....constructors, destructors, other functions....

enum _E_ { P=1, Q, R };

void vFunc(int n);
} ;

class C : public A {
public:
//....constructors, destructors, other functions....

enum _E_ { P=5, Q, R };

void vFunc(int n);
} ;

// --- Main ---

int Main (void) {

B *objB = new B();
C *objC =new C();

A *p = objB;
p->vFunc(P);

p = objC;
p->vFunc(P);

//......some other stuff + delete.......

} //main

Questions:
1. Can you actually do this?
2. will the value of P be correct? (in class B it'd be 1, in class C
it'd be 5)

Thanks everyone!
 
V

Victor Bazarov

FefeOxy said:
Hi,

This is a hypothetical thing that I want to test out, and if it works,
I will be using it to enhance my code.

It doesn't "work".
// --- Suppose I have an abstract base class: ---

class A {
public:
A(void) { }

A() { }

virtual A(void) { }

virtusl ~A() { }
//...some virtual functions....
virtual void vFunc(int n)=0;
};

// --- and 2 class that inherits it.. .---

class B : public A {
public:
//....constructors, destructors, other functions....

enum _E_ { P=1, Q, R };

Do not use identifiers that begin with an underscore and a capital
letter. They are reserved, and you are not allowed to use them.
void vFunc(int n);
} ;

class C : public A {
public:
//....constructors, destructors, other functions....

enum _E_ { P=5, Q, R };

void vFunc(int n);
} ;

// --- Main ---

int Main (void) {

int main() {
B *objB = new B();
C *objC =new C();

A *p = objB;
p->vFunc(P);

'P' is undeclared here.
p = objC;
p->vFunc(P);

'P' is undeclared here.
//......some other stuff + delete.......

} //main

Questions:
1. Can you actually do this?

No, I cannot. And if you want to know if it's allowed, _you_ could
simply pass it through a compiler. You _do_ have a compiler, don't
you?
2. will the value of P be correct? (in class B it'd be 1, in class C
it'd be 5)

There will be no "value of P". 'P' is undeclared in 'main' scope.

V
 

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

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top