Class sharing

This is a discussion on Class sharing within the C++ Programming forums, part of the General Programming Boards category; If i have three classes A, B and C Code: class A { private: B b; C c; }; I ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    5

    Class sharing

    If i have three classes A, B and C

    Code:
    class A {
        private:
    	B b;
    	C c;
    };
    I can create a pointer in class A that can access classes B and C however is it possible for classes B and C to then look back at class A?

    and is it possible for classes B and C to access each others variables and functions?

    I have got around this problem in the past by just making the class a function in side the class it needs to share information with however this is making class A bigger then it should be and causing other sorts of problems, what I am trying to do maybe bad coding practice so any suggestions on a way around this problem is greatly welcome.

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    To make let's say class B to have access to members of class C you just do that:

    Code:
    class C{
    ...
    friend class B;
    //you can also make just one function to have access to class B:
    friend MyFunc(....);
    };
    If you want classes B and C to turn back to A you must declare a pointer to an object of class A like this:

    Code:
    class A;//this is smth like a declaration of a function
    class B{
    A *myPointer;
    //.....
    };
    Sorry if my English is bad.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    B and C can never look at the A instance which contains them, because It impossible to know if they're even contained wherelse. The solution would be to add a friend method to B and C which receives a instance of A
    Last edited by xErath; 06-26-2005 at 09:27 AM.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    They can look at the instance of A, which contains them if they have a poiter of type A and in (for example) the constructor of A somehow we make this pointers to "point" to "this" (which is the object of type A).

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    5
    Ok can someone post some code to show me what you mean as i cant get any of it to work each class has its own cpp and h file if tat makes a differance thanks

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    Code:
    //in B.h
    
    class A;
    
    class B{
    A *ContainingObject;
    ....
           B();
    void SetContaingObject(A *a);
    void ShowContaingObject();
    };
    
    //in B.cpp
    ....
    void B::SetContaingObject(A *a)
    {
    if(a!=NULL)
        ContainingObject=a;
    }
    
    B::B(){
    ContaingObject=NULL;//#define NULL 0x0
    }
    
    //in A.h
    class A{
    ...
    B obj;
    };
    
    //in A.cpp
    
    A::A(){
    obj.SetContainigObject(this);
    ........
    }
    So the object ot type B, which is in the object of type A (obj) can perform operation on the containing object:

    Code:
    //in B.cpp
    void B::ShowContaingObject()
    {
    if(ContaingObject)
       cout<<(void*) ContaingObject;
    }
    I have created the example in the editor of replies, so it may contain syntax errors, but I think the may idea is clear.

    Wish me luck- Tomorrow I have a test on Complex Analisis.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I generally try to avoid this scenario. Instead, I prefer to create a 'Link' object between two objects (of different types), with some mechanisms for querying and sending messages between the two 'ends' of the link.

    From my perspective, this makes for a much cleaner relationship between objects.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    When two classes have pointers to each other, how do you create objects of the classes?

  9. #9
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    You create them as normal
    Then at some point you set the 2 pointers (before using them of course).

    In this scenario you would create object A, then B (most likely within A's constructor). At this point A has a pointer to B (or has the entire instance of B) now A can pass B its this pointer so B can set its pointer to A (this can be done in B's constructor).

    In a more general case each object has a method to set the pointer ot the other and they are called after object creation (possibly by a third object that knows about both)

    Remember that a pointer is not an instance of a class. A pointer is a varialbe that holds the location to an instance of a class. This mean that a pointer might not "point" to any instance at all. This what makes it possible to create 2 objects that have poitners to each other.
    Last edited by sigfriedmcwild; 06-27-2005 at 08:34 AM.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    5
    I still cant get classes and B and C to look at each other data does someone have a code example of this using different files for each class?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 05:30 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Replies: 7
    Last Post: 05-26-2005, 11:48 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21