![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
overload assignment operator with non-member function (assign class to int/dbl/str)apologies for the abbreviations in the title, apparently there is a length limit.
anyways, i have a class Decimal with lots of overloaded operators (I am doing this project as operator overloading practice). The class integrates with int, double, and std::string types. so i have overloaded assignment operators as member functions of Decimal, to assign objects of types Decimal, int, double, and string to Decimal objects. the prototype is: CPP / C++ / C Code:
CPP / C++ / C Code:
Quote:
CPP / C++ / C Code:
i have been banging my head against the wall for quite some time. would somebody please tell me what i'm doing wrong or point me to an accurate operator overloading reference? thanks in advance. EDIT x2: disabled the damn smileys |
|
#2
|
||||
|
||||
|
The assignment operator cannot be a friend function. The assignment operator can only be declared as a non-static member function. This is to ensure that it receives the L-value as its first operand. The same is true for the [], (), and -> operators.
__________________
-Aaron |
|
#3
|
|||
|
|||
|
ok, thanks. so does that mean there is no way to allow statements like this:
CPP / C++ / C Code:
|
|
#4
|
||||
|
||||
|
It is possible to do what you want, without trying to overload assignment operators for other types.
The construct is called the cast-operator. If you define such an operator, then the compiler can implicitly cast one type to the another by use of that method. For example, CPP / C++ / C Code:
I should also mention that in C++, the preferred way of handling casts is to use one of the four C++ cast functions: static_cast, dynamic_cast, reinterpret_cast, or const_cast. So, in addition to defining the cast operator for your class as described above, the assignment statement might look like the following. CPP / C++ / C Code:
Matthew ;-) |
|
#5
|
|||
|
|||
|
thanks aaroncohn and QED. I will implement those cast operators. so i would just have, for example
CPP / C++ / C Code:
with the static_cast<>(), i don't know if that would work because my class Decimal is not a primitive type, and there is no way that the compiler can know the structure of my class. (it's not just a wrapper for a double -- it has three members, left (left of the dec. pt.), right, and sign) |
|
#6
|
||||
|
||||
|
Your welcome. :-)
The syntax for the cast operator does not include a return type, because this is implicit from the cast type. Also, no parameters are needed because only one implicit parameter is required: the pointer this. Similar to the prototype for assignment operator member, which only requires one explicit parameter for the right-hand argument, but the left-hand argument is implicit. Also, static_cast<> works with complex types, provided that the compiler can determine how to convert the complex type to something else. By defining the cast operator member function, you are giving the compiler all the information it requires to perform the static_cast. Matthew |
|
#7
|
||||
|
||||
|
Hey, I learned something new, too. You get rep points.
__________________
-Aaron |
Recent GIDBlog
Welcome to Baghdad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Tutorial] GUI programming with FLTK | dsmith | FLTK Forum | 10 | 03-Oct-2005 15:41 |
| Error C2146: syntax error : missing ',' before identifier 'C4' | mattchew008 | C++ Forum | 2 | 19-Dec-2004 06:06 |
| Help! Some basal questions about MFC | xutingnjupt | MS Visual C++ / MFC Forum | 1 | 05-Dec-2004 03:38 |
| Revising Script style ?????? | pepee | MySQL / PHP Forum | 4 | 14-Apr-2004 04:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The