Thread: Calling a pure virtual function from a constructor...

  1. #1
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45

    Question Calling a pure virtual function from a constructor...

    Hi,

    I'm currently working on a program in which I have a class called IDeviceInfo. About 20 other classes derive from it, so I put a pure virtual function called ResetVariables() in IDeviceInfo, which is then overridden individually in all the different derived classes to reset the required internal variables. I then tried to call ResetVariables from the constructor of IDeviceInfo, so that I wouldn't have to call ResetVariables 20 times in the constructors of the derived classes. This doesn't work though, and results in an error. (Calling a pure virtual function) I have a similar situation in another section of my code where I try to call a pure virtual function from a constructor of a base class as well.

    How can I circumvent this problem?

    As I mentioned above, I am aware that I could just call ResetVariables() 20 times from the constructors of each of the derived classes, but this is not practical and not what I am looking for, for reasons that are too long to explain here. I am also aware that I could abolish the ResetVariables() function and simply put all the 'resetting' code into each of the constructors. I cannot do this however as I need to be able to reset the variables in an instance of any of the 20 derived classes using polymorphism (ie using a pointer to an IDeviceInfo).

    Thank you in advance,

    Philipp
    I program solely as a hobby so this is definitely not homework.
    Best thing that's been said to me:
    I was so pleasantly surprised to see another post in this thread, and yours was brilliant; a fitting end to a subject (matrix manipulations of all kinds) that is quite intriguing.
    Read this thread to find out why...
    Cannibalism

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, you just declared the two possible solutions as not practicable. Seems like you're screwed.

    Virtual calls don't work before the object is completely constructed, period. (And once destruction has started.) Calling pure virtuals leads to a compiler error if it is done directly, and a crash if it's done indirectly. There is no way to work around this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45

    Unhappy Decision...

    Looks like I'll have to call the ResetVariables() function 20 times in that case and try to work around the shortcomings that result.

    Thanks for the response!

    Philipp
    I program solely as a hobby so this is definitely not homework.
    Best thing that's been said to me:
    I was so pleasantly surprised to see another post in this thread, and yours was brilliant; a fitting end to a subject (matrix manipulations of all kinds) that is quite intriguing.
    Read this thread to find out why...
    Cannibalism

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If your code that works with your objects holds a pointer to a dynamically created object, why not use a factory?

    Code:
    IDeviceInfo *DeviceFactory(info_to_select_type input_type)
    {
          IDeviceInfo *object = // create a new object based on value of input_type
          object->ResetVariables();   // reset it
          return object;
    }
    
    int main()
    {
         std::auto_ptr<IDeviceInfo> object(DeviceFactory(whatever_info));
          // work with object
    
         object->ResetVariables();
    
           // work with object ....
    }
    Make all your constructors protected, and any user of your classes is forced to use the CreateDevice() function to create a device.

    Another way is to just destroy an object and create a new one when you need to reset. Although that relies on all constructors setting a sensible default state. It is also prohibitive if the act of destroying or creating the objects is expensive (by whatever measure is relevant to your situation).

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Why not create a factory function which creates the object and then calls ResetVariables() on it before returning it?

    Code:
    template <typename DeviceInfo>
    DeviceInfo *CreateDeviceInfo()
    {
        DeviceInfo *di = new DeviceInfo(...);
        di->ResetVariables();
        return di;
    }
    Then call with:

    Code:
    CreateDeviceInfo<FooDevice>()
    instead of using new directly.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM
  5. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM

Tags for this Thread