Thread: class contains its self pointer

  1. #1
    Registered User
    Join Date
    Feb 2010

    class contains its self pointer

    i want to ask is it normal when I intended to design a class Hierarchy like following?
    class Base {
    class Manager: public Base {
       void init() { _p = new Derived(); }
       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
    Why is Manager derived from Base?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2010
    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
    in Main.cpp
    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
    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
    class Manager
        Manager(Base*);    // simply constructor
        void SetObject(Base*);   // deletes data and then sets this one.
        Base* GetObject() const;  // returns data
        Base* data;
    // later on in your code
    Base * base;
    Manager manager(base);

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, 03:05 PM
  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