Thread: A class in a class

  1. #1
    Unregistered
    Guest

    A class in a class

    A class in another class can reach the first class private members, right? But how? I don't get it to work

    Like:
    class C1
    {
    private:
    int i;
    class C2 {};
    };

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    You can access the private members of the surrounding class from the inner class only if you declare inner classes as a friend of the surround class. You will probably need forward declarations as well.

    for example

    class C1
    {
    class C2; // forward declaration
    friend class C2;
    private:
    int i;

    class C2 {};
    };

    You cannot do that directly in C++ because that requires static scoping and C++ supports only dynamic scoping. You may however use this in Java.

  3. #3
    Unregistered
    Guest
    K. Can I reach it with just i or do I have to refer to the class?

  4. #4
    Darrok
    Guest
    i thought that the "restricted" keywork allowed this

  5. #5
    Unregistered
    Guest

    I think ur right

    I think that Darrok is right on this one. This is a big part of c++ and is known as inheritance. Therefore, if u want to know more about it, search inheritance and c++ on a search engine.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    Actually what I say works for static variables. I don't think that restricted keyword (?) or inheritance will do any difference either. In C++, inner classes are simply name hiding mechanisms. There is no a link to an enclosing object and no implied permission in C++.
    All I know to get the value from outer class is:

    class C1
    {
    class C2; // forward declaration
    friend class C2;
    private:
    static int i;

    class C2 {
    void foo() {
    C1::i = 3;
    };
    };

  7. #7
    Unregistered
    Guest
    Yes, but I don't want to have a static variable...

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    Yes, but I don't think there is another way either. You may use an instance of C1 in C2, which I'm sure you don't want either.

  9. #9
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    A class in another class can reach the first class private members, right?
    No, because it couldn't be guaranteed that the outer class would be constructed first, so the inner class may be accessing data that doesn't exist.

    You can either seperate the two classes, derive C2 from C1 or as ozgulker has pointed out create an instance of C1 in C2 (C2 can then access C1's members because C1 is guaranteed to be created first).
    zen

  10. #10
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    After playing around with some code, there is a way to do what you want, but you'll have to pass in an outer class object or reference to each of the inner class functions. Something like this -

    Code:
    #include <iostream>
    using namespace std;
    
    class C1
    {
    private:
    	int i;
    public: 
    	
    	class C2 
    	{
    	public:
    		C2(C1 &ref) { ref.i=99;}; 
    		void PrintI(C1 &ref){cout << ref.i;}
    	};
    	friend class C2;
    };
    
    
    int main()
    {
    	C1 c1;
    	C1::C2 c2(c1);
    	c2.PrintI(c1);
    
    	return 0;
    }
    zen

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Just that nobody worries...

    There is no "restricted" keyword.

  12. #12
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99
    offcourse it canbe done

    Code:
    class A
    {
        int i;
    public:
        class B;//just a declaration
        friend class B;
        void interface_functions();//
    };
    
    class A::B//because B is in A's scope
    {
        int j;
    public:
        int fn();
    };
    
    void A::interface_functions()
    {
    
    }
    
    int A::B::fn()
    {
        A a;
        a.i+=j;//allows private member access
        return a.i;
    }

    also if for somereason you want to hide the implementation in this inner class then you can put that part in a cpp file and the first cass declaration in a header file this way you can distribute yr library wth the header file and just the obj for the cpp file
    jv

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM