Constructor wont compile

Discussion in 'Mac Programming' started by Aikiman, Feb 23, 2010.

  1. Aikiman macrumors member

    Aikiman

    Joined:
    Jan 24, 2008
    #1
    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
     
  2. mslide macrumors 6502a

    Joined:
    Sep 17, 2007
    #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).
     
  3. Aikiman thread starter macrumors member

    Aikiman

    Joined:
    Jan 24, 2008
  4. Aikiman thread starter macrumors member

    Aikiman

    Joined:
    Jan 24, 2008
    #4
    Would you ever make the parameters of a Constructor pointers or references to the arguments?
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    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
     
  6. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    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? )
     
  7. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #7
    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.
     
  8. Aikiman thread starter macrumors member

    Aikiman

    Joined:
    Jan 24, 2008
    #8
    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.
     
  9. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #9
    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.
     

Share This Page