I have a complex object type for which I'm overriding the "base.ToString()" method so that it returns a string property value.
for example:
class foo
{
public string StringValue{get;set;}
public int SomeOtherValue{ get;set;}
public override ToString()
{
return StringValue;
}
}
So this allows me to use retrieve the value of the StringValue property easily.
Is there a way in which I can override or extend the base functionality so that I can use simple code such as below to set the StringValue
property?
foo aFoo = new foo();
aFoo = "Some string value";
C does not support overloading of operators or functions. There's no way you can redefine < , <= , > , >= , == , or != to compare struct types directly.
Overloading the assignment operator (operator=) is fairly straightforward, with one specific caveat that we'll get to. The assignment operator must be overloaded as a member function. This will call f1. operator=(f1), and under the simplistic implementation above, all of the members will be assigned to themselves.
You can redefine or overload the function of most built-in operators in C++. These operators can be overloaded globally or on a class-by-class basis. Overloaded operators are implemented as functions and can be member functions or global functions. An overloaded operator is called an operator function.
If you return a reference, minimal work is done. The values from one object are copied to another object. However, if you return by value for operator= , you will call a constructor AND destructor EACH time that the assignment operator is called!!
You can use implicit cast operator overload: http://msdn.microsoft.com/library/z5z9kes2.aspx
You can't overload the = operator as stated here
See this page for a complete list of overridable operators.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With