Humam Helfawi
Humam Helfawi

Reputation: 20264

Directly using the constructor of class in other class constructor

I have this:

class point{
public:
    point()=default;
    point(int x,int y):x(x),y(y){}
    int x,y;
}

and this:

class quad{
public:   
    quad()=default;
    quad(point a,point b,point c, point c):a(a),b(b),c(c),d(d){};

    point a,b,c,d;
}

In the main, I can do this:

point a(0,0),b(1,1),c(2,2),d(3,3);
quad q(a,b,c,d);

Or directly this:

quad q(point(0,0),point(1,1),point(2,2),point(3,3));

but of course NOT this:

quad q(0,0,1,1,2,2,3,3); // I know it's wrong

The question:

Is it possible without declaring a new constructor that take 8 integers in quad to use the last code? the motivation of this question is the way that emplace_back works. To be more clear:

std::vector<point> points;
points.push_back(point(0,0)); // you have to pass object
points.emplace_back(0,0);  // you have just to send the arguments of the constructor

Upvotes: 0

Views: 60

Answers (1)

TartanLlama
TartanLlama

Reputation: 65580

This is not possible without declaring a new constructor.

One option is to just pass in brace initializers for each of the points:

quad q({0,0},{1,1},{2,2},{3,3});   

Upvotes: 7

Related Questions