2

I was wondering if there is a difference between the methods of conversion, and if one is better than the other.

Suppose there is a class A and a class B. class B can be converted to a class A. Say for example class A is something representing a string, and class B is something representing an integer.

You could obtain a class A object from a class B object in a number of ways:

  • operator A() function in class B to allow implicit conversion
  • explicit operator A() function in class B to allow explicit conversion
  • A toA() function in class B, to effectively convert it
  • static A parse(B) function in class A to convert it
  • A(B) constructor in class A to create a new object

Is there any preferred way of converting the object? Should multiple ways be implemented? Should any be avoided? Or is it all context-based, and should it be determined by best judgment?

Qub1
  • 1,154
  • 2
  • 14
  • 31
  • Possible duplicate of [Conversion constructor vs. conversion operator: precedence](http://stackoverflow.com/questions/1384007/conversion-constructor-vs-conversion-operator-precedence) – markshancock Nov 26 '15 at 23:48
  • @markshancock I don't think this is about precedence. – deviantfan Nov 26 '15 at 23:50
  • @markshancock Although they seem similar, that question is asking about a mechanism of the language - I want to know, when in a situation where all options are available, which ones should be used / are preferred – Qub1 Nov 26 '15 at 23:54
  • 1
    None of the above? If you can convert `A` to `B` through the use of their public interfaces then make a free conversion function, much like the standard library does with `std::to_string` or `std::stoi`. – user657267 Nov 27 '15 at 00:11
  • @user657267 That's a possability, but why do libraries like Qt still include conversion functions then? – Qub1 Nov 27 '15 at 00:12
  • 1
    @Qub1 Because they're badly designed :p I kid of course, a conversion operator can make sense for value types, and with the advent of `explicit` the problems related to implicit conversions have gone. The great thing about free functions though is they decrease coupling, neither `A` nor `B` would be responsible for the conversion, they don't need to know anything about the other's interface. Use conversion operators if you think they de-clutter your code and are easy for the user to understand (and you aren't worried about the dependency). – user657267 Nov 27 '15 at 00:22
  • @user657267 Okay so basically the functions included in such libraries are there for the convenience of the programmer? – Qub1 Nov 27 '15 at 00:44
  • 1
    @Qub1 absolutely, conversion operators are just syntactic sugar. Use them for small, frequently used value types but don't go overboard or you'll end up with dependencies up the wazoo and god classes. – user657267 Nov 27 '15 at 00:56
  • 1
    @Qub1 To your point, if this is just a class that only you expect to use (not a library) maybe the best choice it to use the choice that you feel fits the style of the code best (makes it easiest to read). I have often chosen the Conversion Constructor or explicit conversion to force the syntax and prevent the compiler from introducing unexpected conversions. If I feel that is unlikely and want the feel to be more language native then I lean toward implicit conversion. – markshancock Nov 27 '15 at 02:40

1 Answers1

0

It's been a while since I posted this question and since then I've developed a few guidelines which I try to stick by in my projects, and I thought I'd share them so others who stumble upon this question can benefit from them as well.

I also welcome any input on the drawbacks of these guidelines, as they are probably not perfect.


  1. An object should only provide implicit conversion constructors for objects that are directly related to that class in some way. For example, a string class that offers a constructor that accepts a char array. When the conversion is lossy, or the classes are unrelated, use an explicit conversion constructor.

  2. An object should only provide implicit conversion operators to objects it is directly related to and for conversions which are lossless (no information is lost). Again, a string can provide a conversion to a character array. If this is not the case, an explicit conversion operator should be used.

  3. Provide toX() functions for all conversion operators (implicit and explicit). This isn't really a strict guideline, but more personal preference. Sometimes it is better to write toX() in your code as it conveys your intent better, in my opinion.

  4. Only use parse(X) functions if the function has to be static for some reason.

  5. Free functions can be used as an alternative to a parse(X) function, this depends on personal preference.


As I said, I welcome any input and I hope this may help someone.

Qub1
  • 1,154
  • 2
  • 14
  • 31