C++ two-way composition

Hi,
I need help implementing two-way composition. Assuming I have two classes:

Class ClassA
{
public:
...
private:
ClassB memberB;
};

Class ClassB
{
public:
...
private:
ClassA memberA;
};

Can I do that? The compiler will generate an error message saying that it does not recognise type "ClassB," since ClassB is declared only after ClassA. One would have to come before the other.
Any help would be greatly appreciated. Thanks.

Comments


  • Hi,
    : I need help implementing two-way composition. Assuming I have two classes:
    : : Class ClassA
    : {
    : public:
    : ...
    : private:
    : ClassB memberB;
    : };
    :
    : Class ClassB
    : {
    : public:
    : ...
    : private:
    : ClassA memberA;
    : };

    : :
    : Can I do that? The compiler will generate an error message saying that it does not recognise type "ClassB," since ClassB is declared only after ClassA. One would have to come before the other.
    : Any help would be greatly appreciated. Thanks.
    :
    : According to what I know, in order to refer to a class -affilitate (object or anything else linked to a class by name) requires a forward declaration, or reference. Look below.
    :

    class B;
    Class ClassA
    : {
    : public:
    : ...
    : private:
    : ClassB memberB;
    : };
    :
    : Class ClassB
    : {
    : public:
    : ...
    : private:
    : ClassA memberA;
    : };
    I hope it works.
  • :
    : Hi,
    : : I need help implementing two-way composition. Assuming I have two classes:
    : : : Class ClassA
    : : {
    : : public:
    : : ...
    : : private:
    : : ClassB memberB;
    : : };
    : :
    : : Class ClassB
    : : {
    : : public:
    : : ...
    : : private:
    : : ClassA memberA;
    : : };
    :
    : : :
    : : Can I do that? The compiler will generate an error message saying that it does not recognise type "ClassB," since ClassB is declared only after ClassA. One would have to come before the other.
    : : Any help would be greatly appreciated. Thanks.
    : :
    : : According to what I know, in order to refer to a class -affilitate (object or anything else linked to a class by name) requires a forward declaration, or reference. Look below.
    : :
    :
    : class B;
    : Class ClassA
    : : {
    : : public:
    : : ...
    : : private:
    : : ClassB memberB;
    : : };
    : :
    : : Class ClassB
    : : {
    : : public:
    : : ...
    : : private:
    : : ClassA memberA;
    : : };
    : I hope it works.
    :

    Hello,

    Well, Forward declaring does not work in this situation.
    The reason, the compiller need class A do forward declare B...So he goes searchig for A and when he find A he discover that he need B to generate A so he check B and see that A is neaded....And so on...

    Pat
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories