Given the constraints, an idiomatic way to do this in C++ would be Solution 2(a). Here's the interface; you have to provide the implementation:
class Kilometers
{
public:
Kilometers(int kilometers);
int value() const;
private:
int kilometers_;
};
class Miles
{
public:
Miles(int miles);
int value() const;
private:
int miles_;
};
void Convert(const Kilometers& kilometers);
void Convert(const Miles& miles);
Does that count as one
Convert
function or two? If two, I'd probably go with Solution 2(c), which could even provide
two outputs if both pointers were valid.
Another question is whether the two constructors should be tagged
explicit
, which is described
here[
^]. Ignore what it says about C++17 and C++20 and just focus on what the presence or absence of
explicit
does when a constructor takes
one argument. I probably wouldn't use it in these cases, but it's always something to decide when a constructor takes one argument.