Sign in to follow this  
jamesbf

Question about derived classes....

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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this