Thread: Class inside Class

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640

    Class inside Class

    Im looking for some help or a tutorial about programming
    classes inside a class

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    What do you mean??? Like this

    Code:
    class foo{
    public:
    class bar{};
    };
    
    int main()
    {
     	foo *a = new foo;
     	
     	foo::bar *b = new foo::bar;
     	
     	delete a;
     	
     	delete b;
     
      	return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Give an example of your design. For example, how are the classes related?

    Kuphryn

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    just a small add-on to fordys code.....
    This is private nesting.
    Code:
    class foo{
    private:
    class bar{};
    };
    
    int main()
    {
     	foo *a = new foo; // fine
     	
     	foo::bar *b = new foo::bar; // compiler error bar is private to foo
     	
     	delete a;
     	
     	delete b;
     
      	return 0;
    }
    so as you see any classes nested in the public side of a class become part of the interface for that class. For instance, STL classes define publically nested iterator classes. Whereas for instance private nesting could be used to hide the implementation details of a node in a linked list. The node need not be part of the interface and is but an implementation detail so this should be hidden in the private side of the class.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Code:
    #include <iostream>
    
    using namespace std;
    
    class abc
    {
    public:
    	class def
    	{
    	public:
    		void ghi();
    	};
    };
    
    void abc::def::ghi()
    {
    	cout << "jkl";
    }
    
    
    void main()
    {
    }



    For example how can i call ghi()? (forgive me for the code above
    but im new to this forum and not quite know how things work)

    [EDIT]Code tags added by Hammer. Please read this thread

  6. #6
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    I'd guess:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class abc
    {
    public:
    	class def
    	{
    	public:
    		void ghi();
    	};
    };
    
    void abc::def::ghi()
    {
    	cout << "jkl";
    }
    
    
    int main()
    {
    	abc::def m;
    	m.ghi();
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Nice! it worked! thanks allot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unresolved external on vector inside class constructor
    By Mario F. in forum C++ Programming
    Replies: 13
    Last Post: 06-20-2006, 12:44 PM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. Declaring an instance of a class inside a class
    By nickodonnell in forum C++ Programming
    Replies: 4
    Last Post: 10-01-2005, 11:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM