0
class car {
    int speed; 
    double position;

    public:
       car(int v,double d);
       int getspeed();
};

int car::getspeed() {
return speed;
}

car::car(int s, double x){
   speed=s;
   position=x;
}

Even though i specified different variables for car(int s,v), why does it work? i though it should give me a compile time error?

this code:which var it uses?

class car {
    int speed; 
    double position;

    public:
       car(int speed,double time);
       int getspeed();
};

int car::getspeed() {
   return speed;
}

car::car(int speed, double position){
   speed=speed;
   position=position;
}

I think the global variable might be used, or is it something you can't say certain about

4
  • actually i tired to do this.speed, and the compiler gave me an error, that "this" is not defined. I remember i was able to do "this" with out including any special headers, it seems now i can't. Commented Feb 1, 2012 at 21:45
  • 1
    How about this->speed = speed. Remember: this is a pointer! Commented Feb 1, 2012 at 21:56
  • Why would the first result in a compilation error? Commented Feb 1, 2012 at 22:04
  • @Rave: this is always a valid symbol in member function scope. Always. Commented Feb 1, 2012 at 22:04

6 Answers 6

7
car::car(int speed, double position){
   speed=speed;
   position=position;
}

In this function definition, it does nothing with the class member car::speed, and car::position, because you declared the local int speed and double position in the function parameter list, they hide the class member variables. To do it properly, you need explicitly say so:

car::car(int speed, double position){
   this->speed=speed;
   this->position=position;
}
Sign up to request clarification or add additional context in comments.

Comments

4

The names of parameteres are not important. Types of parameteres create the signature. The signature is the same, so there is no compile error.

In second example speed in constructor will shadow speed atribute. Therefore you will assign parameter value to parameter variable. You need:

this->speed = speed;

And this is not guesswork ;-).

1 Comment

It took me a minute to figure out what your comment was... I've been in C# too much lately. I made the same mistake in a comment above.
2

This constructor doesn't work

car::car(int speed, double position){
   speed=speed;
   position=position;
}

because it assigns the parameters to themselves.

This version does work because of the slightly odd scoping rules of a class

car::car(int speed, double position) : speed(speed), position(position)
{  }

Comments

1

The compiler doesn't care about your variable names in the method declaration, just the signature, which is

car::car(int,double)

for both your constructor declaration and your implementation, so it knows to match these up when linking. This is possible because you cannot have two methods in the class with the same signature. (You can do this with subclasses, but the result is an override).

Comments

1

In the second case, I believe the closest scope variable is used. So, first it checks the local function scope, and finds both speed and position, so the search stops there. In effect, the second constructor isn't actually assigning obje

Comments

1

In your code:

car::car(int speed, double position){
   speed=speed;
   position=position;
}

you're just assigning each variable's value to itself. You can however do:

car::car(int speed, double position)
  :speed(speed)
  ,position(position)
{}

in addition to explicitly accessing the member variables via this->

1 Comment

if i have a private member int *p; and in the constructor i do this; this->p=new int; to assign the value i end up doing this *p=value; is there a way to do that with "this"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.