c++: calling multi-argument constructor using operator =

Viewed 53

i have this sample:

#include <iostream>
using namespace std;

class A {

public:
    int x;
    A(int one) { x = one; }
    int getX() { return x; }
};

void main()
{
    A first(5);
    first = 10;
}

which in here the constructor is called in both lines of main.

but if we had more than one variable in the class, is it possible to call the constructor with the operator =?

like in here:

class A {

public:
    int x,y;
    A(int one,int sec) { x = one; y=sec;}
    int getX() { return x; }
    int getY() { return y; }
};

and create a class variable using = like this?

A example=(50,40)
1 Answers

Yes, since C++11 you can do it with copy-list-initialization like this:

A example = {50, 40};
example = {40, 50};
Related