hey all!

This is a discussion on hey all! within the C++ Programming forums, part of the General Programming Boards category; ok, heres the thing. I just had a question about nesting classes. I know this is a C board, but ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    hey all!

    ok, heres the thing. I just had a question about nesting classes. I know this is a C board, but to illustrate what i mean i have a short java example.
    Code:
    public class TestClass
    {
     public static class OtherClass
     {
      public OtherClass ()
      {
      }
      public void doSomething ()
      {
      }
     }
     public static class HelpMeClass
     {
      public HelpMeClass()
      {
      }
      public void doSomthingElse
      {
      }
     }
    }
    see how the classes OtherClass and HelpMeClass are nested inside of TestClass? Can you do anything like that in C++? Hey, im new at this. I've never really used C++ classes before. Sorry about the java, but i had no idea how to illustrate it in C++. If this question would be better posted elsewhere, please dont hesitate to say so.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Can you do anything like that in C++?
    Yes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    well, that works for me. like i said, completely new to C++. I just started using it because i like the object oriented approach over C. The textbook im using doesnt cover how to do that. Could you refer me to a better source of information or show me an example or something? Any help is greatly appreciated.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    The syntax is not all that different, actually:
    Code:
    class TestClass
    {
    public:
        class OtherClass
        {
        public:
            OtherClass()
            {
            }
            void doSomething()
            {
            }
        };
        class HelpMeClass
        {
        public:
            HelpMeClass()
            {
            }
            void doSomethingElse()
            {
            }
        };
    };
    Though in this example, TestClass is better as a namespace than a class.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    Thats very helpful, but now i have to ask this question: will all of the classes that are named inside of the main class be subclassses (or derived classes, however C++ puts it), or will they be completely separate from TestClass?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    They will not be subclasses, though they will have access to the members of the outer class (the exact details of which I have forgotten).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    oh, ok. Thats really all i wanted to know. Thanks alot for all your help!

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    No, actually they won't have access. (I think there's a defect report about that.)

    Note that inner classes in C++ are like static inner classes in Java, i.e. you don't need an instance of the outer class to create them, but they can't refer to non-static members of the outer class without a concrete instance.

    To simulate Java's static inner classes, you have to do this:
    Code:
    class Outer
    {
      class Inner;
      friend class Inner;
      class Inner
      {
        // Actual definition
      };
    };
    To simulate non-static inner classes is a bit more effort.
    Code:
    class Outer
    {
      class Inner;
      friend class Inner;
      class Inner
      {
        Outer *outer;
        Inner(Outer *o) : outer(o) {}
      };
    };
    You need to explicitly pass the outer object to the constructor of the inner one. You can then refer to properties of the outer object through the pointer in the inner one.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by laserlight View Post
    They will not be subclasses, though they will have access to the members of the outer class (the exact details of which I have forgotten).
    I don't think they get access unless you declare them friend of the enclosing class..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey, need help with Dropping ball and Bouncing
    By thynksheraze in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2003, 02:05 PM
  2. Hey
    By REMY in forum C++ Programming
    Replies: 1
    Last Post: 01-22-2003, 10:06 PM
  3. Hey all...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-01-2002, 07:56 PM

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