Howdy, Stranger!

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

Categories

C++ two-way composition

briantbriant Member Posts: 2
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

  • beirowerribeirowerri Member Posts: 109

    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.
  • PatLDDDPatLDDD Member Posts: 30
    :
    : 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
  • Justin BibJustin Bib USAMember Posts: 0

    ______ ( http://forcoder.org ) free ebooks and video tutorials about ( Visual Basic .NET, Objective-C, PHP, Java, R, C#, Ruby, Delphi, MATLAB, Visual Basic, Go, Assembly, JavaScript, Perl, C++, C, Scratch, Python, Swift, PL/SQL Lua, COBOL, Clojure, LabVIEW, VBScript, Fortran, Logo, Transact-SQL, Bash, ABAP, SAS, Rust, Awk, Apex, Kotlin, Scheme, Hack, Dart, Erlang, Prolog, F#, Ada, Alice, Crystal, Scala, Julia, ML, FoxPro, D, Lisp ) _________

Sign In or Register to comment.