3

If I have 2 classes A and B and wish to implement conversion from A to B, I can see 2 possibilities.


1st possibility

class A {
    // Some attributes, methods
    operator B() const {
        // Conversion from A to B
    }
}

class B {
    // Some attributes, methods
}

2nd possibility

class A {
    // Some attributes, methods
}

class B {
    // Some attributes, methods
    B& operator =(const A& src) {
        // Conversion from A to B
    }
}

Both these methods allow to run the following code:

Executed code

A instA;
B instB;

instB = instA; // Ok

Now let's imagine I implement both functions (cast to B in class A and operator = from A in class B:

3rd possibility

class A {
    // Some attributes, methods
    operator B() const {
        // Conversion from A to B - Code 1
    }
}

class B {
    // Some attributes, methods
    B& operator =(const A& src) {
        // Conversion from A to B - Code 2
    }
}

assuming Code 1 and Code 2 have the same effect (or even different effects, why not).

My questions are:

  • If competing cast/assignment methods are provided, which one would be chosen first when doing implicit cast?
  • Is there any interest implementing both competing cast/assignment methods?
  • Perhaps this would even be a good idea to call operator = of class B in B() cast operator of class A?
Community
  • 1
  • 1
Benjamin Barrois
  • 2,566
  • 13
  • 30
  • Related, and possible duplicate: [Conversion operator vs constructor from given type. Which is preferable?](https://stackoverflow.com/questions/6095802/conversion-operator-vs-constructor-from-given-type-which-is-preferable) – xskxzr Feb 26 '18 at 16:08
  • @xskxzr Related definitely, duplicate no, my questions are multiple and go further. – Benjamin Barrois Feb 26 '18 at 16:14
  • Also related/dupe: [Conversion constructor vs. conversion operator: precedence](https://stackoverflow.com/questions/1384007/conversion-constructor-vs-conversion-operator-precedence). – jdehesa Feb 26 '18 at 16:15

1 Answers1

2

If competing cast/assignment methods are provided, which one would be chosen first when doing implicit cast?

Given the choices, the operator=() will be given the first preference since it does not require the argument, the RHS, to be converted.

Is there any interest implementing both competing cast/assignment methods?

No. That's best avoided to prevent confusion.

Perhaps this would even be a good idea to call operator = of class B in B() cast operator of class A?

If you must support both, then, yes, it would be a good idea to do that.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thanks for your answer. For the last point I really meant *in*, not *and*. Another question: can you overload `operator =` of native types (int, long, etc) to set a conversion from your own class? – Benjamin Barrois Feb 26 '18 at 16:25
  • @BenjaminBarrois, If by that you mean `int i; B b; i = b;`, no you can't do that. – R Sahu Feb 26 '18 at 16:33
  • So how do you explain the execution of the following code?https://onlinegdb.com/S1nk_nZOG – Benjamin Barrois Feb 26 '18 at 16:38
  • @BenjaminBarrois, that is not a `operator=` overload. It is a user defined conversion operator. Yes, the language definitely supports that. – R Sahu Feb 26 '18 at 16:43
  • Oh ok, sorry I thought you were saying it wasn't possible to do `int i; B b; i = b;` in general. – Benjamin Barrois Feb 26 '18 at 16:45
  • @BenjaminBarrois, no problem. It's good that we cleared up that confusion. – R Sahu Feb 26 '18 at 16:48