• ### Popular Now

• 15
• 15
• 11
• 9
• 10

#### Archived

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

# Strange constructor..

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

## Recommended Posts

I ran across this constructor, but I don''t understand how it works. Can someone enlighten me?

class MD2
{
private:
MD2_MODELVERTEX vertList[100];

int numGlCommands;
long* glCommands;

int numTriangles;

public:
int stateStart;
int stateEnd;

int frameSize;
int numFrames;
char* frames;

int currentFrame;
int nextFrame;
int endFrame;
float interpolation;

MD2() : stateStart(IDLE1_START), stateEnd(IDLE1_END),
numGlCommands(0), frameSize(0), numFrames(0),
currentFrame(IDLE1_START), nextFrame(currentFrame+1),
endFrame(IDLE1_END), interpolation(0.0f)
{	}

};

What exactly is the '':'' doing? I''ve used it when calling parent constructs..but here it''s calling some variables in some strange way..I''m confused..
/* Everything IMHO */ -Luctus

##### Share on other sites
Its initializing variables.
This bit here: MD2() : stateStart(IDLE1_START), stateEnd(IDLE1_END) means "MD2 constructor. Start by setting stateStart to IDLE1_START, and stateEnd to IDLE1_END".
Most people put this code in the body, but theres some things that have to be initialized in this way, like references (because they need to point to a valid object from construction), and calling the base class constructor.

##### Share on other sites
When you construct an object from a class it goes through the members of the class and constructs them. You can give those members their initial values in an initialiser list. This is more efficient than constructing the members with a default constructor and then going through them setting them to the values you really want. Also in some cases it''s necessary (ie when a member variable is of a class type that has no default constructor).

The members are created in the order they are declared in the class declaration not in the order of the initialisation list, so that''s something to watch out for, especially if you''re using one value to initialise another.

peace