Creating an instance of an object & inheriting from that creator object

This is a discussion on Creating an instance of an object & inheriting from that creator object within the C++ Programming forums, part of the General Programming Boards category; Hi, Im having serious problems with this. Ive got an class called newCtl. This class needs to create another different ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    45

    Creating an instance of an object & inheriting from that creator object

    Hi,

    Im having serious problems with this. Ive got an class called newCtl. This class needs to create another different object instance when it is created (i.e through its constructor). The object to be instanced is called newPrj_win.

    Now c++ doesnt seem to allow me to inherit (i.e newPrj_win: public newCtl) from an object that created the object in the first place. I need it to do this as the origional object is a control class giving functionaility for the child class to use

    Its hard to explain so please bear with me. Im using wxWidgets btw as my gui.

    Cheers
    Alex

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Can you write in code what you want to do (that the compiler doesn't allow)?

    The solution might be to change the inheritance hierarchy. Another solution might be to use a pointer for newPrj_win inside the newCtl class that can be null when there is no newPrj_win to be stored.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    Ok ill do it in code roughly...

    Code:
    class newCtl{
         public:
                newCtl()
         private:
                newPrj_win *new_window
                launch_ui();
    };
    
    ********************
    newCtl::newCtl(){
    
        launch_ui();
    }
    
    *********************
    newCtl::launch_ui(){
         new_window = new (blah ... class constructor etc)
    
    };
    
    *******************
    *******************
    //Now the basic structor of the newPrj_win class
    
    class newPrj_win: public wxFrame, newCtl{
    
    //This code is good
    
    }
    The above is quickly typed as i dont have the actual code to hand. basically I need to create newPrj_win dynamically in memory because of the way wxWidget works.

    Cheers
    Alex

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That should compile ok, assuming you forward declare newPrj_win above the newCtl class.

    In that code, you are recursively creating newCtl objects. This recursion will never end. If your actual code is like this you will have to rethink it a bit. Notice how when you create a newCtl, it runs the newCtl constructor, which always calls launch_ui(), which always allocates space for a newPrj_win, which of course will always call the newPrj_win constructor, which will always call the base class constructor for newCtl, which always calls launch_ui(), and so on and so forth.

    As long as you have some way of only allocating space for a newPrj_win when you really need it inside newCtl, you should be fine.

    Whether this is all good design is another question, but there isn't really enough information for me to comment on that.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    Quote Originally Posted by Daved
    That should compile ok, assuming you forward declare newPrj_win above the newCtl class.

    In that code, you are recursively creating newCtl objects. This recursion will never end. If your actual code is like this you will have to rethink it a bit. Notice how when you create a newCtl, it runs the newCtl constructor, which always calls launch_ui(), which always allocates space for a newPrj_win, which of course will always call the newPrj_win constructor, which will always call the base class constructor for newCtl, which always calls launch_ui(), and so on and so forth.

    As long as you have some way of only allocating space for a newPrj_win when you really need it inside newCtl, you should be fine.

    Whether this is all good design is another question, but there isn't really enough information for me to comment on that.
    Ok thanks,

    At the end of the day I need 2 classes. One control class that deals with functionaility, & one UI class that provides the UI element to this functionaility...

    How would you go about doing this?

    Many Thanks for your help

    -Alex

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another question :O need ideas and confirmation
    By Akkernight in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 02:29 PM
  2. Which one is "quicker" ?
    By AloneInTheDark in forum C# Programming
    Replies: 2
    Last Post: 02-08-2008, 08:23 PM
  3. Creating object of type HWND from a dll
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-12-2002, 11:40 PM
  4. Replies: 3
    Last Post: 12-03-2001, 12:45 PM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM

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