Sign in to follow this  
Mizipzor

Constructor taking arguments?

Recommended Posts

Mizipzor    247
I got my sdl class who takes three arguments, x and y resolution and the bit depth. That class is a member of my "master" class, the class the holds everything together and handles the main loop. However in the master class where I try to make an instance of the sdl class I get:
"error C2059: syntax error : 'constant'"
Heres how I try to do it:
class Game {
private:
	Sdl_handle	sdl(resX, resY, 32);	// for the graphics
ResX and Y are defined at the top of the header. What am I doing wrong? Cant I pass arguments from within a class header? In that case, how do I create it? The constructor needs arguments...

Share this post


Link to post
Share on other sites
snk_kid    1312

class Game {

Sdl_handle sdl;

//....

public:

Game(int resX, int resY)
: sdl(resX, resY, 32) {} // constructor initializer list

//....

};

Share this post


Link to post
Share on other sites
Endurion    5412
If you want to provide parameters to a member class you have to do that in the constructor of the parent class via initializer lists:

In your .h:

class Game
{

public:

Game( int resX, int resY );

private:

Sdl_handle sdl;

};


In your .cpp:

Game( int resX, int resY ) :
sdl( resX, resY, 32 )
{
}



Edit: bad snk_kid, stop posting faster than me!

Share this post


Link to post
Share on other sites
Staffan E    536
Try declaring a pointer to an object of the class and create it at runtime:
// In the header
class Game {
private:
Sdl_handle *sdl;
};

// In the constructor of Game
Game::Game()
{
sdl = new Sdl_handle(resX, resY, 32);
}

// In the destructor of Game
Game::~Game()
{
delete sdl;
}
Stay sharp.

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by staaf
Try declaring a pointer to an object of the class and create it at runtime:


Erm that is unnecessary, try looking at the last two posts [wink].

Share this post


Link to post
Share on other sites
Staffan E    536
Quote:
Original post by snk_kid
Quote:
Original post by staaf
Try declaring a pointer to an object of the class and create it at runtime:


Erm that is unnecessary, try looking at the last two posts [wink].


I could not. They were in writing as i typed my post. Since my post wasn't a repost I decided not to reomve it, even though your solution/s seem more slick. [smile]

Share this post


Link to post
Share on other sites
Mizipzor    247
Thanks. :)

My constructor now looks like this:

Game::Game() : sdl( resX, resY, 32 ) // setup resolution
{
// set up initial player position
pos.X = 50;
pos.Y = 50;

// the app is running
running = true;
}



What is the : doing there? :P Commands before the constructor? Are those carried out before the class is created?

Share this post


Link to post
Share on other sites
Endurion    5412
The : declares the initializer list. This is called, after your class is created, but before the members constructors are called.

It's the only way to call non-default constructors of member classes without a late assignment.

You could also do an assignment in the constructors body. In that case the member is created with its default constructor and then in the body gets assigned a new instance. A direct construction in the initializer list gets rid of that extra step (which can be quite costly on bigger objects).

Share this post


Link to post
Share on other sites
Sharlin    864
Also, initializer lists are the *only* place where you can call a non-default base class constructor and initialize constant or reference member variables.

For consistency's sake, you should initialize all your members in initializer lists, whenever possible:


Game::Game() :
sdl(resX, resY, 32),
// assuming pos has a suitable constructor,
// if not, you should probably write one
pos(50, 50),
running(true)
{
}

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