Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Aikiman

macrumors member
Original poster
Jan 24, 2008
33
0
Im wondering why this code wont compile

Code:
Enemy::Enemy(int hitpoints, int skill) :
	int m_hitpoints(hitpoints),
	int m_skill(skill)
{}

m_hitpoints and m_skill are declared in the private bin of my class Enemy, but my compiler complains that it expects an identifier before int and hitpoints is not declared in this scope.

Cheers
 

mslide

macrumors 6502a
Sep 17, 2007
707
2
Change it to this:

Code:
Enemy::Enemy(int hitpoints, int skill) :
	m_hitpoints(hitpoints),
	m_skill(skill)
{}

That is, remove the int's before m_hitpoints and m_skill. You only need the 'int's when declaring a variable and those 2 variables are declared in your class definition (in your header file most likely).
 

Aikiman

macrumors member
Original poster
Jan 24, 2008
33
0
Would you ever make the parameters of a Constructor pointers or references to the arguments?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Would you ever make any method take a pointer? If so, why not here?

What you do need to be aware of is passing a stack-resident variable via pointer/reference, store the pointer, then keep the constructed object after the stack frame gets popped. This is true for any pointer to any stack-resident variable. If you store it somewhere that is on the heap, or in a higher stack frame.

Ways around this are copying the value, or only passing heap-resdient variable via address.

-Lee
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Im wondering why this code wont compile

Code:
Enemy::Enemy(int hitpoints, int skill) :
	int m_hitpoints(hitpoints),
	int m_skill(skill)
{}

Try to explain to us (and to yourself) what the stuff behind the ":" is supposed to do. If you can't explain it, get a book teaching C++. If you can explain it, then you know why the "int" before m_hitpoints doesn't make sense. (You didn't add an "int" in front of Enemy::Enemy either. Why? )
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Would you ever make the parameters of a Constructor pointers or references to the arguments?

What does this mean? "Parameters" are the parameters to any function (and a constructor is just a special case of a function), from the point of view of the function. "Arguments" is what the caller passes to the function. So during the call, "Arguments" change to "Parameters", but that is no real change, just the point of view changes from caller to the callee.

Do you mean "would you every use parameters in a constructor that are of type "pointer to something" or of type "reference to something""? Yes, why not? You'd do that with any function, so why not with a constructor?

Actually, it is very common that a class X has a constructor that takes X& as a parameter, and that constructor is expected to construct the object by copying things from the object that was passed via reference as much as possible.
 

Aikiman

macrumors member
Original poster
Jan 24, 2008
33
0
Try to explain to us (and to yourself) what the stuff behind the ":" is supposed to do. If you can't explain it, get a book teaching C++. If you can explain it, then you know why the "int" before m_hitpoints doesn't make sense. (You didn't add an "int" in front of Enemy::Enemy either. Why? )

Im not sure what ":" means but I do know that the operator appears right before any initializes in a constructor and it also appears when you derive a class from another class. I will look it up and find out, cheers.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Im not sure what ":" means but I do know that the operator appears right before any initializes in a constructor and it also appears when you derive a class from another class. I will look it up and find out, cheers.

Before the code that you write yourself in your constructor is executed, the compiler must execute constructors for all objects inside your object. If you don't do anything, it will execute all the default constructors, and the default constructors for plain C objects like int, double, or pointers do nothing.

Behind the colon in the constructor, you write a list of constructors for members of your object that you want to be called. So "m_hitpoints (hitpoints)" tells the compiler: Before executing the code in { braces } that I wrote, call the constructor for the member m_hitpoints with an argument of hitpoints, which will set m_hitpoints = hitpoints, instead of calling the default constructor for m_hitpoints which does nothing.

For plain C members this doesn't make much difference, because you could have just written the assignment m_hitpoints = hitpoints in your constructor instead. For members that are proper C++ objects in their own rights it is better telling the compiler which constructor to use after the ":" because inside your constructor code there is no way anymore to call constructors for your member objects.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.