C++ derivatives problem

M

Maurice

Hello,

Please take a look at this well-known 'problem':

----------------
class A{
B * foo();
};

class B{
A * foo();
};
----------------

Its solution is simple: add "class B;" above A.


But here's a smilar, but more complex, problem:

----------------
class A_1 {
virtual B_1 * foo();
};

class B_1{
virtual A_1 * foo();
};


class A_2 : public A_1 {
virtual B_2 * foo(); //Overrides foo() of A_1. The return-type is
different, but it's okay because B_2 is a derivative of B_1
};

class B_2 : public B_1 {
virtual A_2 * foo(); //Overrides foo() of B_1. The return-type is
different, but it's okay because A_2 is a derivative of A_1
};
----------------

Adding "class B_1; class B_2;" on top of it doesn't solve the problem,
because the compiler only accepts the first override if it knows that B_2 is
a derivative of B_1.

How do I tell the compiler that B_2 is a derivative of B_1, without having
to specify all the contents of B_2 ?

Thanks in advance,
-Maurice-
 
V

Victor Bazarov

Maurice said:
Please take a look at this well-known 'problem':

----------------
class A{
B * foo();
};

class B{
A * foo();
};
----------------

Its solution is simple: add "class B;" above A.


But here's a smilar, but more complex, problem:

----------------
class A_1 {
virtual B_1 * foo();
};

class B_1{
virtual A_1 * foo();
};


class A_2 : public A_1 {
virtual B_2 * foo(); //Overrides foo() of A_1. The return-type is
different, but it's okay because B_2 is a derivative of B_1
};

class B_2 : public B_1 {
virtual A_2 * foo(); //Overrides foo() of B_1. The return-type is
different, but it's okay because A_2 is a derivative of A_1
};
----------------

Adding "class B_1; class B_2;" on top of it doesn't solve the problem,
because the compiler only accepts the first override if it knows that B_2 is
a derivative of B_1.

How do I tell the compiler that B_2 is a derivative of B_1, without having
to specify all the contents of B_2 ?

Your question has no answer. Similar to "how can humans live on the
Moon without special suits or shelter?"

V
 
L

LR

I don't know for certainty, but I suspect you can't do that.
Your question has no answer.

There are however, practical solutions that may work for the OP.

Perhaps change
B_2 * A_2::foo();
to
B_1 *A_2::foo();

I don't know if this will work for you, but if you were intending on
returning a pointer to a B_2, then you can return a pointer to a B_1.

You might also consider writing a wrapper for a B_1 pointer, and writing
overloads for operator-> and whatever else you need. Maybe B_1 and B_2
get ctors that take a B1Pointer as an argument. Change the A_1::foo()
and A_2::foo() to return an instance of the B_1 pointer wrapper class.


Your question has no answer. Similar to "how can humans live on the
Moon without special suits or shelter?"

By adding enough mass to the moon to allow for an atmosphere? I
anticipate an extinction level event on earth. ;)

LR
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,282
Latest member
RoseannaBa

Latest Threads

Top