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 I've got 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 the contents (the function foo()) of B_2 ?

Thanks in advance,
-Maurice-

Edit - Never mind, I didn't read the OP correctly

As far as I understand the standard, this is not possible .

There are slightly upsetting work-arounds.

First: If you don't need the virtual aspect, this is easy use a template return type. Easy.

Second: If you require the virtual part you can then return A_1 from B_2::foo. [You can get one part to return a A_2/B_2]. And then you have to cast is up later, since you can write virtual A_1* foo() { return new A_2; } BUT I loath the dynamic_cast that is going to be required if A_1 and A_2 are not access only via virtual methods [e.g. A_1 is not an interface class].

However, IF you are accessing an object of B_2 via a virtual function , you have to expect that you will not get a A_2 point back and you will be accessing the points methods via virtual methods and the conversion to A_2 doesn't matter.

Anyway , not really a solution, just some random thoughts. Sorry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.