Thread: Accessing classes over classes

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    24

    Accessing classes over classes

    Hey!

    I've been fighting with a problem about using members of a class. I haven't got anything working solution of it. I've tried searching from google too. Probably I just don't know what to search for... Can you help because I know just "ugly" ways of doing this? I'd prefer wise way.

    The problem (not real code):
    Code:
    class C {
    public
       getC()
    private
       objC
    }
    
    class B : public class C {
    public
       getB()
    private
       C objB
    }
    
    class A : public class B {
    public
       getA()
    private
       B objA
    }
    
    main {
       A object
       C object2 = object.getA().getB().getC()
    }
    So, I'd like to get to class B or class C in not so ugly way as that. Should I inherit the class B as private, "private class B", or something? Or use protected?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    NB: My opinions follow:

    It might be better if you gave a concrete example of what your actually trying to do. Are you really just using classes "A", "B", "C"? Or are you using real-world "things", like "Animal", "Dog", "Cat", etc? I would try and think of a more concrete example of what your trying to do. I cant recall any time Ive had to use a design such as what your doing (whether in this language or not). Do you really need a "B" to store a reference to its parent class "C", without "C" being "*this"? Again, I cant think of a reason why you would need that sort of thing.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    24
    In real code I'm trying to make a console program. I have classes "Application", "Normal_screen", "Clock_window", "Clock" and "Curse_window" in this hierarchy:

    Application
    |_Normal_screen
    | |_Clock_window
    | | |_Curse_window
    | | |_Clock
    | |_Main_window
    |_Inventory_screen

    So, I have for example in application.h "class Application : public Normal_screen, public Inventory_screen". I'm not sure if I'm thinking right about this problem like I should. Feels like there are two ways of thinking this, logical and hierarchical...hard to explain, but anyway...

    My plan is that there are many windows' on screen (Normal_screen). In this example there's just one, Clock_window which is made window by Curse_window. In Clock_window I want to show a clock. I'd like to give commands to Clock_window from main() without ugly sentences that I wrote about earlier.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Use this:
    http://en.wikipedia.org/wiki/GRASP_(...riented_Design)

    One suggestion, for example:

    Code:
    class C {
    };
    
    class B {
    public:
       getC();
    private:
       C objC;
    };
    
    class A {
    public:
       getC();
    private:
       B objB;
    };
    
    int main {
       A object
       C object2 = object.getC();
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing classes
    By Mavix in forum C# Programming
    Replies: 2
    Last Post: 02-16-2008, 09:43 PM
  2. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM
  3. 2 Classes Accessing Eachother
    By Chronom1 in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2003, 05:10 PM
  4. Replies: 8
    Last Post: 07-27-2003, 01:52 PM
  5. Accessing Classes in Classes
    By GrNxxDaY in forum C++ Programming
    Replies: 18
    Last Post: 07-30-2002, 01:54 PM