class CPlayer{
public:
void CPlayer(int h, int a, int d)\\constructor???
{
health = h;
attckpwr = a;
defpwr = d;
}
private:
int health;
int attckpwr;
int defpwr;
};
Is this the right way to use a constructor?
Is this the right way to use a constructor?
Cory Fisher
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
January 15, 2003 09:00 PM
yep... although you might want to use the correct //
commenting style instead of \\
commenting style instead of \\
Generally, yes. However, if some of the members you''re initializing are classes rather than integers, or even here to make your code more standard, initializer lists are a better option:
oh, and slashes for comments are forward slashes, not backslashes.
Don''t listen to me. I''ve had too much coffee.
class CPlayer{public: void CPlayer(int h, int a, int d) : health(h), attckpwr(a), defpwr(d) { }private: int health; int attckpwr; int defpwr;};
oh, and slashes for comments are forward slashes, not backslashes.
Don''t listen to me. I''ve had too much coffee.
That''s fine, except that since you are defining a constructor yourself, the compiler won''t create a default constructor for you. There must always be a constructor that can be called without supplying any arguments.
So, either add another constructor with no parameters, or add default values to the one you have, i.e.
void CPlayer(int h=0, int a=0, int d=0)
{
health = h;
attckpwr = a;
defpwr = d;
}
So, either add another constructor with no parameters, or add default values to the one you have, i.e.
void CPlayer(int h=0, int a=0, int d=0)
{
health = h;
attckpwr = a;
defpwr = d;
}
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter
Plasmadog made a good point. However, if you end up defaulting all those arguments, it''s probably best to make the constructor explicit. Also, plasmadog, ctors don''t have a return type
Oops so used to msvc++ i didn''t realize it. With msvc++ it changes the color of the slashes if it''s a comment I sometimes get them confused.
Anyways what is this:
class CPlayer{
public:
CPlayer(int h, int a, int d):
health(h);
attckpwr(a);
defpwr(d);
{
}
private:
int health;
int attckpwr;
int defpwr;
};
Whats with the : i''ve never seen that before nor have i seen health(h) either. thanks i''ll try it.
Cory Fisher
Anyways what is this:
class CPlayer{
public:
CPlayer(int h, int a, int d):
health(h);
attckpwr(a);
defpwr(d);
{
}
private:
int health;
int attckpwr;
int defpwr;
};
Whats with the : i''ve never seen that before nor have i seen health(h) either. thanks i''ll try it.
Cory Fisher
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Constructor initialization list. It''s just another way to initialize things (probably prefered). Also, () expression can be used to initialize variables in normal situations as well. ie,
int x(0);
MSVC++ 6 has a problem using this expression with pointers, so watch out for that.
int x(0);
MSVC++ 6 has a problem using this expression with pointers, so watch out for that.
I never learned how to use pointers anyway. Something like *pvalue. Thanks
Cory Fisher
Cory Fisher
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
You don''t have-to have a default constructor, though often it''s a good idea. Sometimes you do not want a default ctor (abbrievation for construtor, dtor for destructor), so that the user must supply some parameters. It depends on the intent of the class. If you can reasonably supply a default ctor, you should.
The initializer list is the preferred method to initialize member variables - it avoids potential redundancy, where the variables are assigned to twice.
If you inherit the class from another one, you can "call" it''s constructor in the initializer list to supply it with parameters.
If you do not invoke a particular ctor of your base-class, it will automatically invoke the default ctor (if there is no default ctor, it will fail to compile).
e.g.
class MyPlayer : public CPlayer
{
MyPlayer() : CPlayer(1000, 1000, 1000)
{}
};
...
You sure? I do not recall having difficulty with that. Perhaps it is fixed in one of the service packs? (SP5 was the last one for VC6)
The initializer list is the preferred method to initialize member variables - it avoids potential redundancy, where the variables are assigned to twice.
If you inherit the class from another one, you can "call" it''s constructor in the initializer list to supply it with parameters.
If you do not invoke a particular ctor of your base-class, it will automatically invoke the default ctor (if there is no default ctor, it will fail to compile).
e.g.
class MyPlayer : public CPlayer
{
MyPlayer() : CPlayer(1000, 1000, 1000)
{}
};
...
quote:
MSVC++ 6 has a problem using this expression with pointers, so watch out for that.
You sure? I do not recall having difficulty with that. Perhaps it is fixed in one of the service packs? (SP5 was the last one for VC6)
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
alright alright... I don´t know a lot but I think that you should take the constructor out from the class. I was teached to make the class in a .h and then the ctor in a different file, a .cpp ...
Forgive me if i´m wrong but I think this is the way :
(I don´t know how to put the programing sheet, sorry)
class CPlayer{
public:
void CPlayer1(int, int, int);
private:
int health;
int attckpwr;
int defpwr;
};
//and the in the .cpp
#include"the file abobe"
//when you have inicializate the vars ...
void CPlayer::CPlayer1(int h, int a, int d)
{
health = h;
attckpwr = a;
defpwr = d;
}
****
yeah, it´s a bit more longer but if you are making something big you should try to be more ... correctly i think...
if i´m wrong forgive me..
I´m just a poor boy nobody loves me...
Forgive me if i´m wrong but I think this is the way :
(I don´t know how to put the programing sheet, sorry)
class CPlayer{
public:
void CPlayer1(int, int, int);
private:
int health;
int attckpwr;
int defpwr;
};
//and the in the .cpp
#include"the file abobe"
//when you have inicializate the vars ...
void CPlayer::CPlayer1(int h, int a, int d)
{
health = h;
attckpwr = a;
defpwr = d;
}
****
yeah, it´s a bit more longer but if you are making something big you should try to be more ... correctly i think...
if i´m wrong forgive me..
I´m just a poor boy nobody loves me...
numbloq@yahoo.comI´m limitated to ask only... so, don´t ask for my opinion.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement