c++ - 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 main, can 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); // know it's wrong
the question:
is possible without declaring new constructor take 8 integers in quad
use last code? motivation of question way emplace_back
works. more clear:
std::vector<point> points; points.push_back(point(0,0)); // have pass object points.emplace_back(0,0); // have send arguments of constructor
this not possible without declaring new constructor.
one option pass in brace initializers each of points:
quad q({0,0},{1,1},{2,2},{3,3});
Comments
Post a Comment