What it means in C# ?
In C#, the semantics is not about overloading the operator =
(which you can't), but provide a cast operator from any type to the type A, or from the type A to any type.
This means the following code (completed with the A-to-type conversions):
class A {
public static implicit operator A(string s) {
return new A();
}
public static implicit operator A(double d) {
return new A();
}
public static implicit operator string(A a) {
return string.Empty;
}
public static implicit operator double(A a) {
return 0.0;
}
static void Main(string[] args) {
A a = "hello"; // line A
A b = 5.0; // line B
a = "World"; // line C
a = 3.14; // line D
double d = a ; // line E
string s = a ; // line F
}
}
works for assignment, either in simple assignments (like lines C and D) or assignment in declarations (like lines A and B). The line E and F demonstrate how we can convert from the user type to other types.
How to do it in C++ ?
In C++, the lines A and B are object constructions, which will call the relevant constructors.
The lines C and D are assignment, which will call the relevant assignment operator.
So the C++ code for the C# code you provided must provide both constructors and assignments for string and doubles, as follows:
class A
{
public:
A(const std::string & s) { /*...*/ }
A(double d) { /*...*/ }
A & operator = (const std::string & s) { /*...*/ ; return *this ; }
A & operator = (double d) { /*...*/ ; return *this ; }
// etc.
} ;
This way, you can have
void main()
{
A a0 = "Hello" ; // constructor
A a1("Hello") ; // constructor
A a2 = 5.0 ; // constructor
A a3(5.0) ; // constructor
a0 = "Hello World" ; // assignment
a0 = 3.14 ; // assignment
}
What about cast operators in C++ ?
The cast operators in C++ work like the following C# converter operators:
class A
{
static public operator string(A a) { /*... ; return a string */ }
static public operator double(A a) { /*... ; return a double */ }
// etc.
}
In C++, the cast operators are written as such:
class A
{
public:
operator std::string() { /*... ; return a string */ }
operator double() { /*... ; return a double */ }
// etc.
} ;
void main()
{
A a ;
std::string s ;
double d ;
// etc.
s = a ; // cast operator
d = a ; // cast operator
}
Why is it so complicated in C++ ?
In C#, a cast/conversion operator can be written from the class type to any type, or from any type to the class type.
In C++, for conversion, you must choose between one or more ways, that is: constructor, assignment operator or cast operator, because in C++, you have a fine grained control on types and operation, and thus, you must use that fine grained API.
Constructions means the object does not exist, so there is no point in constructing it to some default value and then assigning it another value (this can cost in speed). In C#, the reference/value type is null/zeroed before the assignment, so this is not an issue.
Assignment means the object does already exist, so it is possible one can reuse the same internals to accommodate the new value. In C#, the original reference object is discarded, and another is created (if the creation costs, that you'll pay that price)
As for the cast, it is used in the current case to convert an existing class into another for which you have no control: You have no right to extend the std::string to accommodate a constructor for your class, and there is no way to add a constructor to a built-in type like double.