For example:
Code: Select all
class foo
{
foo()
};
foo::foo :
poo( stuff )
{
}
-Paradox
Code: Select all
class foo
{
foo()
};
foo::foo :
poo( stuff )
{
}
Code: Select all
foo::foo
: // initialize some simple variables ( I think it is the only thing you can do here,
// someone correct me if Im wrong )
pooX( x ),
pooY( y ),
pooZ( z )
{
// here you are free, you can initialize things in the way you want,
// using loops, function calls etc.
// initialize an array of numbers
for( int index = 0; index < 10; index++ )
{
numbers[index] = index;
}
// load an image
loadImage( "picture.bmp" );
// initialize a simple variable
gameover = false;
}
Right, when a class-object is created it's class constructor is called. The stuff behind the colon is not of an integral type, so what is it? What I've always done is stuff like this:Far as I understand, the constructor is called just once when the program starts, so yes, the purpose is to initialize your variables or functions.
Code: Select all
foo::foo
{
bar = 10;
// Do stuff with bar...
}
Code: Select all
foo::foo :
bar( stuff )
{
// Do stuff.
}
Code: Select all
foo::foo
: bar( 10 ),
x( 150 ),
y( 200 )
{}
Code: Select all
foo::foo
{
bar = 10;
x = 150;
y = 200;
for( int index = 0; index < 5; index++ )
{
numbers[index] = index;
}
}