Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write constructor for a library class that doesn't have one

I am using a struct from a 3rd party library to pass parameters into library functions.

I wish there were a constructor that would allow me to quickly create a struct, but the library doesn't provide one.

Is there some way to define a constructor outside of the library that I can use within my own code? If not, does anybody else see an elegant solution, here?

My motivation is that I would save some speed because I wouldn't have to construct each struct member twice.

Also, my code would be more readable because I could condense struct creation into one line instead of many lines.

I want to go from this:

Point newPoint;
newPoint.x = someXValue;
newPoint.y = someYValue;

To this:

Point newPoint(someXValue, someYValue);
like image 686
user2445507 Avatar asked Feb 23 '26 05:02

user2445507


1 Answers

If you're using C++11, you can construct as:

Point newPoint {someXValue, someYValue};

and if you're not, assuming this is a POD structure, you could:

Point newPoint = {someXValue, someYValue};
like image 111
user666412 Avatar answered Feb 25 '26 17:02

user666412