Classes and variables scope in C++

后端 未结 6 1561
一整个雨季
一整个雨季 2020-12-22 06:32
class car {
    int speed; 
    double position;

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

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

car::car(         


        
6条回答
  •  被撕碎了的回忆
    2020-12-22 06:51

    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 ;-).

提交回复
热议问题