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

## Recommended Posts

Hey guys, Ive done quite a few tutorials on OOP and think im starting to get the hang of it, but theres one little thing that keeps cropping up in other peoples code that ive never had explained to me. As I dont know what its called I cant really search for it...figured id be better off asking in here. Say you have a class called CParticleSystem, then a derived class (from CParticleSystem) called CSmoke. ie class CSmoke : public CParticleSystem Whats the deal with the constructor for CParticle system being called outside the body of the CSmoke constructor function (ie after the ":") CSmoke::CSmoke(CVector origin, GLuint texture) : CParticleSystem(100, texture) { this->origin = origin; } I have a feeling that it has something to do with the fact that CSmoke is derived from CParticleSystem....if someone could explain this to me id be much appreciative.. (eg can you only do this with constructors?)

##### Share on other sites
The reason they do this is because CParticleSystem doesn't have a default constructor (a constructor that takes no parameters). I think in a case like this it will actually give you errors, because CSmoke MUST call its parents constructor.

Update: I should explain some more info. When you create an object that is a derivative, in this case CSmoke, the constructors of all it's parents classes are called.

So what happens when it tries to do that in this case, where its parent's constructor requires parameters? Well nothing, cause it can't just make up parameters to send. So you must implicitly tell it what to do.

You can actually do other stuff with it as well like initialize members:
// I think my syntax might be off here, sorry.CSomeClass() : m_iNum(100){}

Hope that's enough info!
Matt Hughson

##### Share on other sites
It's called an "initializer list" (which Google). Constructing a CSmoke involves constructing a CParticleSystem, since a CSmoke contains a CParticleSystem. The initializer list lets you specify how the CParticleSystem is constructed.