Unable to determine initialization type

I came across this code and realized that I could not remember the type of initialization used and its intended behavior.
Point p=(3,2);it seems to pass the last value (2 in this case) as an argument to the constructor, so PrintOut shows something like x=2 y=5unlike the expectedx=3 y=2

class Point{
public:
    Point(int x=5,int y=5):a(x),b(y){};
    void printOut()const{
        cout<<"x= "<<a<<"y= "<<b<<endl;}
private:
     int a,b;
};

void main(){
    Point p=(3,2);
}
+4
source share
2 answers

Because it Point p = (3, 2);does not cause the designer with two arguments, but rather causing it to one argument 2. This is because of the comma operator , which basically 3returns the result of the first expression ( ) and returns the last ( 2).

, 2 5.

, :

  • Point p(3, 2);
  • Point p = Point(3, 2);
  • Point p = { 3, 2 }; (++ 11)
  • Point p{ 3, 2 }; (++ 11)
+4

Point p = (3,2) Point p = 2, , , Point p = Point(2), , . ++.

+1

All Articles