class contains its self pointer

This is a discussion on class contains its self pointer within the C++ Programming forums, part of the General Programming Boards category; Hi i want to ask is it normal when I intended to design a class Hierarchy like following? Code: class ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    class contains its self pointer

    Hi
    i want to ask is it normal when I intended to design a class Hierarchy like following?
    Code:
    class Base {
    ...
    };
    
    class Manager: public Base {
    ...
       void init() { _p = new Derived(); }
    private:
       Base *_p;
    
    };
    
    class Derived: public Base {
    ...
    };
    I am at a situation that I think the only solution is have a reference to DerivedB object in DerivedA.

    Just in case someone will curious about how this occurs.
    Actually I am using a third party source code. And he defines an entry point which I need to pass a parameter in Base*... Normally, simply derive from Base, and pass the instance to it will be fine. However, I am trying to design a flexible codes, and I want to create a manager class (which I define as above). And make original user defined class in its data member.
    However, I think if I design this way, it looks just so unreadable for the maintainer.
    I need advice.
    Really appreciate for comments. Thx

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    Why is Manager derived from Base?
    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
    Feb 2010
    Posts
    32
    Quote Originally Posted by laserlight View Post
    Why is Manager derived from Base?

    Manager class derived from the Base is because that third party package defines a entry point
    in main.cpp

    Something like
    Code:
    in Main.cpp
       RunApp(MyObj());
    However, the RunApp needs to pass a Base*. Therefore I have no choice everything I need to pass a reference in the Base class Hierarchy.
    My intention to create Manager class is I try to use Manage class as Contexts in design patters of State Pattern. Thus the state reference(which is DerivedB as I mentioned) can
    change dynamically. But DerivedB needs some parameter in Base class. So it derive from Base class, too.

  4. #4
    Registered User NathanOliver's Avatar
    Join Date
    Jun 2010
    Posts
    2
    Well you manager class doesn't need to inherit from the base class it only needs an object of type Base*. you would do something like
    Code:
    class Manager
    {
    public:
        Manager(Base*);    // simply constructor
        void SetObject(Base*);   // deletes data and then sets this one.
        Base* GetObject() const;  // returns data
    private:
        Base* data;
    };
    
    // later on in your code
    Base * base;
    Manager manager(base);
    RunApp(manager.GetObject());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. 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
  3. Initializing a pointer in a class.
    By gpr1me in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2006, 02:05 PM
  4. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 10:27 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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