Thread: class schema implementation doubt

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    1

    class schema implementation doubt

    Hi ,

    This is the scenario of the problem I am trying to solve ..

    A class 'A' has an object of class 'B'


    From the object of class B , I need to tell the object of class A, to execute a particular function.

    Could you help me with the schema?

    I get compiler errors, when I try to implement this.

    I have uploaded the the schema I tried..

    Thanks,

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If A contains an instance of B, it must know all about B. Therefore B must come first (or included from a header).

    B however doesn't contain an instance of A (it couldn't). Instead it just has to know that a class by the name A exists, so it can declare pointers to it. For that forward declarations are used.

    All in all the layout would be:
    Code:
    //forward declaration of A
    class A;
    
    //declaration of B
    class B
    {
        //...
    };
    
    //declaration of A
    class A
    {
        //we know all about B:
        B b;
        //...
    };
    
    //now we also know all about A and can implement the function in B
    //that needs to know about A's member functions
    void B::set_pointer(A* a) {...}
    Other notes:
    main() should have an explicit return type (C++ does not accept defaulting return types).

    A little indentation wouldn't hurt.

    Code:
    A a= A();
    This, of course, is the same as
    Code:
    A a;
    except potentially calling one constructor and one destructor less.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implementation of set class in C++
    By hay_man in forum C++ Programming
    Replies: 10
    Last Post: 09-21-2006, 03:33 PM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. Hide class implementation from driver?
    By wbeasl in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2004, 05:38 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM