Archived

This topic is now archived and is closed to further replies.

Samith

syntax question

Recommended Posts

if any of you have the book "OpenGL Game Programming" then you can find the snippet im showing you in SimpEngine.h for on chapter21''s source. anyway, i just dont know what this syntax means:
CSimpEngine(const char *szName, bool fscreen, int w, int h, int b) :
		CEngine(szName, fscreen, w, h, b) 
{
...
}
[code]

CSimpEngine is derived from CEngine, that is CSimpEngines constructor    

Share this post


Link to post
Share on other sites
It's called an initialization list. It makes the compiler emit a call to the CEngine constructor with the arguments given before the code-block making up the CSimpEngine constructor is entered.

If you didn't include the ": CEngine(....)"-bit, then the compiler would automatically emit a call to the *default* constructor (the one taking no arguments) of CEngine before the CSimpEngine constructor's code-block was entered (since base-class constructors are always called when constructing an object of a derived class).

The initializer list is also a common place to initialize member-data.

[edited by - Dactylos on July 17, 2002 4:34:00 PM]

Share this post


Link to post
Share on other sites
Ya see, if you have a class that inherits from another class, when you create an instance of it, two constructors are being called: your class, and the base class. Normally, the base class default constructor (no parameters) is called. If you want to call it with something else, you use that syntax.


  
#include <iostream>
using namespace std;

class A {
public:
A() { cout << "A::A() called.\n"; }
A(int i) { cout << "A::A(int i) called; i is " << i << ".\n"; }
};

class B : public A {
public:
B() { cout << "B::B() called.\n"; }
B(int i) : A(i+1) { cout << "B::B(int i) called; i is " << i << ".\n"; }
};

int main()
{
B b1;
B b2(3);
}

Share this post


Link to post
Share on other sites