Thread: Classes inside Classes

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    Classes inside Classes

    I was wondering if someone could give an example of how to use something like:
    Code:
    class TestClass
    {
    public:
             TestClass();
             ~TestClass();
             class Test
             {
                  int x;
                  Test();
                  ~Test();
                  void Test2(int x);
             };
             void Test3(int x);
    };
    or something like that.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    class Test
             {
                  int x;
                  Test();
                  ~Test();
                  void Test2(int x);
             };
    
    class TestClass
    {
    public:
             TestClass();
             ~TestClass();
             Test test;
             void Test3(int x);
    };
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    2
    that only works for public functions then right? if it was somehow declared inside the other class couldn't it use the private declerations too? (assuming there were private declerations) or would the need inheritance?
    Last edited by LostGeneration; 11-16-2003 at 03:30 AM.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by bennyandthejets
    Code:
    class Test
             {
                  int x;
                  Test();
                  ~Test();
                  void Test2(int x);
             };
    
    class TestClass
    {
    public:
             TestClass();
             ~TestClass();
             Test test;
             void Test3(int x);
    };
    There's nothing wrong with inner classes...used in the right circumstances, they can be quite usefull.

    To use the OPs original code (with a few practical changes)

    Code:
    #include <iostream>
    
    class TestClass
    {
    public:
    	TestClass(){}
    	~TestClass(){}
    	class Test
    	{
    	public:
    		int x;
    		Test(){}
    		~Test(){}
    		void Test2(int x_){x = x_;}
    	};
    	int x;
    	void Test3(int x_){x = x_;}
    };
    
    int main(int argc, char *argv[])
    {
    	TestClass::Test foo;
    	foo.Test2(10);
    	std::cout << foo.x << std::endl;
    
    	TestClass bar;
    	bar.Test3(20);
    	std::cout << bar.x << std::endl;
    }

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    So it's like a namespace is it? You can group together a bunch of things?

    BTW, you forgot to return from main() .
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by bennyandthejets
    So it's like a namespace is it? You can group together a bunch of things?
    The inner class can be addressed like a namespace, but it can be handy when you want a private inner class that's more or less invisible to the user of the outer class.....have a peek at this thread I did a while ago on proxy classes - http://cboard.cprogramming.com/showt...ighlight=proxy

    Originally posted by bennyandthejets
    BTW, you forgot to return from main() .
    You dont need to if your compiler is fairly C++ complient. Only old non complient compilers like VC++6 force the explicit return from main.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The most common usage I've seen for nested classes is to hide the implementation of the class completely. For example:

    Code:
    // In the header file.
    class A
    {
    public:
      // ... Stuff ...
    private:
      struct data;
      data* data_ptr;
    };
    Now, the 'data' struct is implemented in the source file. 'data_ptr' is the only (except in exceptional circumstances) private data member of A. In a large project, this means that if you change the internal data of the class, you only need to recompile the class and relink the program, not relink everything that depends on A.h
    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
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I've seen classes declared within classes to set up classes for exceptions to throw like OutOfRange and such.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. College Classes
    By nubby in forum C Programming
    Replies: 2
    Last Post: 10-07-2007, 12:32 AM
  2. Questions on Classes
    By Weng in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2003, 06:49 AM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM