• Advertisement
Sign in to follow this  

Calling specific constructor of derived base class...

This topic is 4854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement