Results 1 to 9 of 9

Thread: Nested Classes

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154

    Nested Classes

    how do you access the inner class in a nested class through the outer class
    PHP Code:
    class Game{

          public:
          class 
    Ball
    i have the class game and the inner class ball; i want too access the Ball member functions through the class Game like this.
    Game.Ball.Member_Function();
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  2. #2
    Lively Member
    Join Date
    May 2000
    Posts
    84
    You need to create an instance of Ball in Game. When all you have is the definition the most you can do is define a instance of ball from game, like this

    Code:
     
    Game g;
    g.Ball b;
    b.MemberFunction();
    In order to use ball in game you must declare a ball object to use like this.
    Code:
    class Game
    {
    public:
        Game();
        ~Game();
    
        class Ball
        {
         public:
              void Member_Function();
        }
    
        Ball b;
    }
    
    Game g;
    g.b.Member_Function();

  3. #3
    Destined Soul
    Guest
    I'm curious - what's the reason for having a nested class? I'd just do:
    Code:
    class Ball
    {
      ...
      void myFunc();
      ...
    };
    
    class Game
    {
       ...
       Ball myBall;
       ...
    };
    
    and something like:
    
    void main()   // or wherever...
    {
       Game myGame;
       ...
    // this way, you can call it like you want:
       myGame.myBall.myFunc();
       ...
    }
    Destined

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154

    Smile

    No reason; i just thought that you could use the ball nested class as a member of Game;
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  5. #5
    amac
    Guest
    If you have a nested class you do not _have_ to declare it within the at class. If it is defined as public you can declare an instance of the nested class anywhere you want...

    class MyClass
    {
    public:
    class Nested
    {};

    };


    int main()
    {

    MyClass::Nested nestedTest;

    return 1;

    }


    This works...

  6. #6
    Destined Soul
    Guest
    Strange, isn't this entirely defeating the purpose? Why not just do:
    Code:
    class Ball{};
    
    class Game
    {
       Ball b;
    };
    Isn't this the same as your example, amac?

    Does anyone know what nesting a class allows or does? From my point of view, it just seems to be a more readable coding style to declare them seperately.

    Although, it has been about 3 years since I've actually read through a book on C++, so I may have once known the answer.

    Destined

  7. #7
    amac
    Guest
    I am not exactly sure here but it makes sense...

    You might be able to define the class as being private/protected...

    therefore it can only be used with the class it was defined... if i am wrong here someone correct me?

    I guess it is just another level of encapsulation...

  8. #8
    amac
    Guest
    an example:

    class Base
    {
    private:
    class Nested
    {};

    protected:
    Nested Ball;

    };

    int main()
    {

    Base test; // would declare an object
    // of Nested inside it...

    Base::Nested test2; // error.

    return 1;

    }

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The STL containers use it.
    All iterators are nested classes, because although they have similar interfaces(++-operator and so on), they need different implementations(you can't cycle a double linked list the same way as an array).
    Code:
    template <class T>
    class vektor
    {
    public:
         class iterator
         {
              int pos;
          public:
              T Next() {return vektor[++pos];};
              T Prev() {return vektor[--pos];};
         }
    }
    
    template <class T>
    class list
    {
          class node
          {
                node* prev;
                T value;
                node* next;
                friend class list::iterator;
          }
    
    public:
         class iterator
         {
               list::node *currentNode;
          public:
               T Next() {currentNode = currentNode->next; return currentNode};
               T Prev() {currentNode = currentNode->prev; return currentNode};
         }
    }
    (of course, this is only an expectation)
    As you see, vektor::iterator and list::iterator behvae the same, although they have different implementations.

    All the buzzt
    CornedBee

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width