Humam Helfawi February 2016

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

Answers


TartanLlama February 2016

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

Post Status

Asked in February 2016
Viewed 3,757 times
Voted 9
Answered 1 times

Search




Leave an answer