Managing Implicit Type Conversion in C++ -
I'm working on code that does the closest neighbor questions. There are two simple ideas that a user can ask for the query of the search data:
- The nearest N points to the point given in space.
- Distance.
In my code, the point is placed in a point list, and the point list is the task of keeping track of points near a container that has been found in search.
Right now my point list object is a constructor:
point list (maximum maximum unsigned); The next two constructors I want to add are: Point list (float maxdist); // # 2 point list (unsigned integer max, float maxdist); // # 3
My question is how can I ensure that my users and C ++ compilers will generate the right constructor for the pointylist and the difference between consultants 1 and 2 ? Can I execute just 3 and give the constants that defines arbitrary big values for Maxwell and MaxDist? Another option could write a second system of light objects that controls the argument to add points to the list, but it seems that overkill for such a simple idea.
I am actually trying to make this transparent, my users, who are mostly scientist who have ever learned C ++ without the benefit of formal education. Thanks!
The overload resolution for integer types is done on two categories, which are very briefly
- Promotion: This is less than one type of conversion from
inttointorunsigned int, depending on it Can I store all the values of theintsource type. - Conversions: This is a conversion from any integer type to another integer type.
Similarly, the conversion for floating point types is on two categories
- promotion: this will be
floattoConversion is a conversion: it is conversion from any floating point type to another floating point typeand the floating or back of the integer is Instead of promotion it is a conversion, it is ranked as a conversion. A promotion is better than a conversion, and where only one promotion is required, that situation will be given priority. In this way, you can use the following constructors:
Point list (Intex Velways); Pointlist (unsigned integer maximum); Point list (long maxville); Point list (unsigned long maxwells); Point list (double maxdist); Point list (long double maxdist);For any integer type, select this first set of constructor. And for any floating point type, it should select the second group of constructors. For example, if you pass
int, then your original two constructors can easily remove the result of ambiguity betweenfloatandunsigned int. For the second, two logic constructors, if you wish, you can go with your solution. He said, I will also use the factory function, because this type of determining parameter is quite delicate, I think most people would expect to be equal to the following result < / P>Point list P (floor (1.5)); Point list u ((int) 1.5);But the result will be in different cases.
Comments
Post a Comment