Inheritance declaration

J

jan247

my header file looks like this:

class A;
class B;
class C;

class A
{
....
};

class B:public A
{
private:
B doSomething(A& a);
public:
B processMe(C c){...; doSomething(c);} // Problematic statment
};

class C: public A
{
....
}

The problem is that by the time it reaches the line of code with the
problematic statement, it hasn't declared yet that C indeed extends A.
Is there any way on how I can declare this? Or any other possible
options if i can't?
 
I

Ivan Vecerina

: my header file looks like this:
:
: class A;
: class B;
: class C;
:
: class A
: {
: ....
: };
:
: class B:public A
: {
: private:
: B doSomething(A& a);
: public:
: B processMe(C c){...; doSomething(c);} // Problematic statment
: };
:
: class C: public A
: {
: ....
: }
:
: The problem is that by the time it reaches the line of code with the
: problematic statement, it hasn't declared yet that C indeed extends A.
: Is there any way on how I can declare this? Or any other possible
: options if i can't?

You have to provide the definition(=implementation)
of processMe after class C has been seen, for example:

class B:public A
{
private:
B doSomething(A& a);
public:
B processMe(C c); // (forward-)declaration
};

class C: public A
{
....
};

B A::processMe(C c) {...; doSomething(c);} // definition



(you could use reinterpret_cast, but this would formally be UB)


Ivan
 
J

jan247

Ivan said:
: my header file looks like this:
:
: class A;
: class B;
: class C;
:
: class A
: {
: ....
: };
:
: class B:public A
: {
: private:
: B doSomething(A& a);
: public:
: B processMe(C c){...; doSomething(c);} // Problematic statment
: };
:
: class C: public A
: {
: ....
: }
:
: The problem is that by the time it reaches the line of code with the
: problematic statement, it hasn't declared yet that C indeed extends A.
: Is there any way on how I can declare this? Or any other possible
: options if i can't?

You have to provide the definition(=implementation)
of processMe after class C has been seen, for example:

class B:public A
{
private:
B doSomething(A& a);
public:
B processMe(C c); // (forward-)declaration
};

class C: public A
{
....
};

B A::processMe(C c) {...; doSomething(c);} // definition



(you could use reinterpret_cast, but this would formally be UB)


Ivan

Ahhh.. thanx!
 
R

red floyd

my header file looks like this:

class A;
class B;
class C;

class A
{
....
};

class B:public A
{
private:
B doSomething(A& a);
public:
B processMe(C c){...; doSomething(c);} // Problematic statment
};

class C: public A
{
....
}

The problem is that by the time it reaches the line of code with the
problematic statement, it hasn't declared yet that C indeed extends A.
Is there any way on how I can declare this? Or any other possible
options if i can't?

Is there a particular reason that you're passing C by value rather than
by const reference? If you use a reference, you can do this:

class C;

class B : public a
{
///
public:
B processMe(const C&);
};


Also, separate the declaration and defintion of B::processMe().
 

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

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top