Advertisement

Class constructor problem

Started by November 15, 2000 02:20 PM
12 comments, last by nino 24 years, 2 months ago
You can''t initialize those members in a class definition, you must do it in the constructor. Try this, it should work:

class CPong
{
public:
CPong();
~CPong();

private:
CPaddle Player1;
CPaddle Player2;
};

CPong::CPong() :
Player1("images\\bluechucks.bmp"),
Player2("images\\yellowchucks.bmp")
{}
quote: Original post by nino

how can i make it so that i can call the CPaddle constructor automatically when the CPong class is created but still have the CPaddle instance usable throughout the whole life of the CPong class?


define the Player1 and Player2 as pointers. Then create them by using new within the constructor of the CPong class (and delete in the destructor) and this particular problem is solved.



cmaker- I do not make clones.
Advertisement
Just use pointers and create the paddles in the CPong constructor :

  class CPong{	public:	CPong()	{		Player1 = new Paddle("images/bluechucks.bmp");		Player2 = new Paddle("images/yellowchucks.bmp");	}	~CPong()	{		delete Player1;		delete Player2;	}	private:	CPaddle *Player1;	CPaddle *Player2;};  


This way the paddles are built when the CPong is built.


--------
Andrew
thanks alot guys i finally got it working i really appreciate the help

Thansk
NiNo

This topic is closed to new replies.

Advertisement