Calling specific constructor of derived base class...

Started by
1 comment, last by CJM 19 years, 3 months ago
Hey. I'm writing a class for my engine at the moment which is basically the wrapper that the designer fits specific components of the game into. Now, I have two specific classes defined sort of like such:

class Object   //I'm coding this
{
public:
   Object(const char* objName);
public:
   virtual void Method()=0;
};

class DesignObject : public Object  //design lead is coding these
{
public:
   DesignObject();
public:
   void Method();
};

class AnotherDesignObject : public Object
{
public:
   AnotherDesignObject();
public:
   void Method();
};

Design objects are compile-time globals [they are the only compile-time globlas in the project]. Now, I know that this code won't let me declare objects [it will give an error saying that Object has no appropriate default constructor]. Is there a way to be able to, for example, say: AnotherDesignObject anotherObject("Another Design Object"); and have it call the constructor of class Object with "Another Design Object" as the name, with minimal coding required in each of the design objects [because I figure engine coding is the programmer's task, not the designer's]... The method that I'd been using previously was to make the object with a default constructor and then to initialise it in a specifically written DesignObjectsInit() function, but when more design code files are added this gets a little messy. Is there a better way? //End rant CJM
Advertisement
Dead simple - assuming I've understood your question correctly:

class Object   //I'm coding this{public:   Object(const char* objName);public:   virtual void Method()=0;};class DesignObject : public Object  //design lead is coding these{public:   DesignObject(const char* objName) : Object(objName) {};public:   void Method();};


The initialiser list sends the required parameters to the base object by directly passing those objects through the base-objects constructor. I've left out the second object as I assume it's irrelevant to this question (although this is why I may be misundertanding your question).

Oh yeah - please have a look at std::string - will potentially remove a lot of problems with using char arrays.

Jim.
JimPrice,

Thanks. I never really learnt the basics of inheritence, so I'm not so surprised that it could be so clean. That was exactly what I wanted.

rating++;

Thanks again,

CJM

This topic is closed to new replies.

Advertisement