Thread: Pointer to class itself

  1. #1
    Cdumbie
    Guest

    Pointer to class itself

    Hi;
    Can anyone help me to understand and illustrate what this
    program do?
    Code:
    class Foo
    {
       public: 
        void get();
       private:
        Foo * a_foo;  // what it is trying to do here???
        Foo    other_foo;
        int x; 
    };
    Can a class has data members of that class or point to that class. I have never seen this before. Your help is grateful.
    Cdumbie

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
    class Foo
    {
       public: 
        void get();
       private:
        Foo    *a_foo;             //This is legal
        Foo    other_foo;        //This is illegal
        int x; 
    };
    A class can have a member that is a pointer to the same type, but it cannot have a member that is an object of the same type.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    A class having a pointer to another class is very useful in linked lists (thats the basic concept of a LL). You cannot have a class that have the same class inside it, this would lead to an infinite recursion when createing that object:
    You create an object of class A, that contain an object of class A, that contain an object of class A, that contain an object of class A, that contain an object of class A... (you get the point ).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    > This is illegal.

    Yeh quite right. A class inside a class, inside a class, inside a class, inside a class, inside a class, inside a class, inside a class, inside a class, inside a class, inside a class, inside a class, inside a class, inside a class, inside a class, inside a class, ...

    >what it is trying to do here???

    It's holding a pointer to another object of the same class. This is not illegal because it is only a pointer, it could even point to itself.


    Also what can do legally is this:

    Code:
    class Foo
    {
       public: 
        void get();
       private:
        static Foo stat_foo;  // Only one instance
        int x; 
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Initializing a pointer in a class.
    By gpr1me in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2006, 03:05 PM
  3. class passing a pointer to self to other class
    By daioyayubi in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2005, 09:25 AM
  4. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM