Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring two different methods which use the same parameters

Tags:

c++

I have two methods to set the construct a parallelogram:

parallelogram(tiny_vec<float, 3> p1, tiny_vec<float, 3> p2, tiny_vec<float, 3> p3);
parallelogram(tiny_vec<float, 3> p1, tiny_vec<float, 3> height_vec, tiny_vec<float, 3> width_vec);

The first creates it out of three points and calculates the fourth. The second takes one point and two vectors and calculates the other three points from that. However, both, points and vectors, are stored as a 3d vector tiny_vec<float, 3>

Is there any convention how to handle this? Merge them into one method and add a bool use_height_and_width parameter? Add a useless parameter to one and give it a default value?

Haven't found any answers, just the opposite question (different types, one method)

like image 818
PattuX Avatar asked Feb 16 '26 02:02

PattuX


2 Answers

Give the functions different names which make the meaning of the parameters clear. If they are constructors, make them instead appropriately named static member functions which return parallelogram objects.

See the Named Constructor Idiom.

like image 163
Benjamin Lindley Avatar answered Feb 17 '26 16:02

Benjamin Lindley


Make points and vectors different types. This also has the benefit of helping to catch errors such as adding two points together.

like image 33
Brian Bi Avatar answered Feb 17 '26 16:02

Brian Bi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!