// A.h
class A
{
private:
const X move_()
{ ... }
public:
const B f()
{ return B( move_() ); }
};
// B.h
class B
{
private:
const X move_()
{ ... }
public:
const A f()
{ return A( move_() ); }
};
> Hi,
> how could following problem be solved (not the same base class)?
What base class?
> // A.h
>
> class A
> {
> private:
> const X move_()
> { ... }
>
> public:
> const B f()
> { return B( move_() ); }
> };
>
>
> // B.h
>
> class B
> {
> private:
> const X move_()
> { ... }
>
> public:
> const A f()
> { return A( move_() ); }
> };
Don't implement the functions in the header, but in the implementation file.
Then put a forward declaration before the class definition.
> Oliver Kowalke wrote:
>
>> Hi,
>> how could following problem be solved (not the same base class)?
>
> What base class?
I mean I don't want to have a (shared) base class (as a suggestion)
>> // A.h
>>
>> class A
>> {
>> private:
>> const X move_()
>> { ... }
>>
>> public:
>> const B f()
>> { return B( move_() ); }
>> };
>>
>>
>> // B.h
>>
>> class B
>> {
>> private:
>> const X move_()
>> { ... }
>>
>> public:
>> const A f()
>> { return A( move_() ); }
>> };
>
> Don't implement the functions in the header, but in the implementation
> file. Then put a forward declaration before the class definition.
forward declarations work for references or pointer in the header - doesn't
the compiler need to know the size of A / B in the header because it A and
B are returned by value and not as refernce or pointer?
>> Don't implement the functions in the header, but in the implementation
>> file. Then put a forward declaration before the class definition.
>
> forward declarations work for references or pointer in the header -
> doesn't the compiler need to know the size of A / B in the header because
> it A and B are returned by value and not as refernce or pointer?
No.
And if they must be inline, do this (with type X defined appropriately):
// A.h
struct B;
struct A {
const X move_() { ... }
const B f();
};
#include "B.h"
inline const B A::f() { return B( move_() ); }
// B.h
struct A;
struct B {
const X move_() { ... }
const A f();
};
#include "A.h"
inline const A B::f() { return A( move_() ); }
With proper include guards for A.h and B.h, the circular inclusion will
work properly.
// X.h
struct X
{ };
// A.h
class B;
class A
{
private:
const X move_()
{ return X (); }
public:
A (X = X ())
{ }
const B f();
};
// B.h
class A;
class B
{
private:
const X move_()
{ return X (); }
public:
B (X = X ())
{ }
const A f();
};
// A.cpp
#include "A.h"
#include "B.h"
const B A::f()
{ return B( move_() ); }
// B.cpp
#include "A.h"
#include "B.h"
const A B::f()
{ return A( move_() ); }
// main.cpp
int main ()
{
A a;
B b;
A aa = b.f();
B bb = a.f();
}