Thread: class A uses class B, and B uses A

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    9

    class A uses class B, and B uses A

    hi, im having some problem wih my classes,

    class A has an pointer of type B.

    and class B has a object of type A.


    Code:
    //FILE A.h
    Class A{
    	public:
    		B* pb;
    };
    
    //FILE B.h
    Class B{
    	public:
    		A objecta;
    };

    they are in different files, i've tried adding class B; before defining class A but i didnt work, i think because they are in separate files.

    posted also in gamedev.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, A does not need B's definition. But it needs B's declaration, so you can use a forward declaration:
    Code:
    //FILE A.h
    
    class B;
    
    Class A{
    	public:
    		B* pb;
    };
    Then you include A's header in B:
    Code:
    //FILE B.h
    #include "A.h"
    
    Class B{
    	public:
    		A objecta;
    };
    Of course, the relevant header inclusion guards have been omitted as in your original example.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    9
    great!

    that helped me understand more about forwarding class declarations, now is all working fine!.

    thank you very much.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that this won't work if you create objects, and not pointers, because the constructor and destructor must be called, but since you only have a forward declaration, the compiler cannot see your full class definition and can't call the constructor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    Note that this won't work if you create objects, and not pointers, because the constructor and destructor must be called, but since you only have a forward declaration, the compiler cannot see your full class definition and can't call the constructor.
    You can't even compile it if you aren't simply forward declaring a pointer (or reference?) to the class.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Huh?
    You can declare pointers to a class to which you've added a forward declaration. Only, you must remember to include the real header in the source file which dereferences that pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You guys/gals are saying the same thing . . . .

    I didn't think you could declare references based on forward declarations -- until I tried it. (Deja vu . . . .)
    Code:
    #include <iostream>
    
    class b;
    class a { class b &bb; };
    class b { class a &aa; };
    
    int main() { return 0; }
    On the other hand, this does not work:
    Code:
    #include <iostream>
    
    class b;
    
    class a {
    public:
        class b &bb;
        void set() { std::cout << "a::set()\n"; }
        void setother() { bb.set(); }
    };
    
    class b {
    public:
        class a &aa;
        void set() { std::cout << "b::set()\n"; }
        void setother() { aa.set(); }
    };
    
    int main() {
        a aa;
        b bb;
        aa.set();
        aa.setother();
        bb.set();
        bb.setother();
        return 0;
    }
    (Sorry, Elysia.)

    I guess it follows the same principles as declaring pointers to forward-declared objects: you can do it, but you can't access anything inside the object without having a full declaration.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, don't inline functions in the header. It's... error prone.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I just did it because it's much more convenient to type into cat.

    Besides, that's not a header -- it's a declaration of a class (or two classes, I suppose); but it's not in a header file, it's in a source file.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So then you should actually include the headers with the class definitions instead of forward declaring the classes!
    Your inlining is why the second sample won't work, as you are aware...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed