GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Apr-2005, 15:29
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
Angry

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:
class Decimal {
...
bool operator=(const int&);
...
};
and this works fine. however, i also want to be able to assign a Decimal to an int. i tried:
CPP / C++ / C Code:
class Decimal; //forward declaration so i can use it in the prototype
/* line 29 */ bool operator=(int&, const Decimal&); //actual prototype
...
class Decimal {
...
/* line 46 */ friend bool operator=(int&, const Decimal&); //friend so it can access private member variables of Decimal
...
};
however, i get the errors:
Quote:
Originally Posted by Dev-C++ 4.9.9.2
29 C:\Program Files\Dev-C++\decimal.h `bool <unnamed>::operator=(int&, const <unnamed>::Decimal&)' must be a nonstatic member function
46 C:\Program Files\Dev-C++\decimal.h `bool <unnamed>::operator=(int&, const <unnamed>::Decimal&)' must be a nonstatic member function
EDIT: everything is contained in a
CPP / C++ / C Code:
 namespace { ... } 
so that's where the <unnamed>:: in the errors is coming from

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  
Old 05-Apr-2005, 01:56
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 05-Apr-2005, 05:00
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
ok, thanks. so does that mean there is no way to allow statements like this:
CPP / C++ / C Code:
Decimal dec(4, 5); //create Decimal 4.5

//i know i can do this
double d = 3.7;
dec = d;

//but can I do this?
double d = dec; //would set d to 4.5
  #4  
Old 05-Apr-2005, 06:59
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
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:
class Decimal
{
  // other member declarations...

public:
  operator double()
  {
    // put definition here
  }
};
On the surface, this method is potentially not as efficient as an assignment operator because this operator returns a value. So when you say something like "double d = dec;" using the method above, there is the overhead of a call to the copy constructor that need not occur if we could overload the assignment operator for this special case. (Of course, for primitive types such as int or double this is negligible.) I do not whether or not good optimizing compilers might reduce this overhead, but the performance impact should be negligible unless you are performing such casts with great frequency, using complex classes rather than primitive types.

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:
double d = static_cast<double>(dec);
This is instead of the implicit cast that relies on the compiler to figure out what to do.

Matthew ;-)
  #5  
Old 05-Apr-2005, 13:43
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
Thumbs up

thanks aaroncohn and QED. I will implement those cast operators. so i would just have, for example
CPP / C++ / C Code:
class Decimal {
...
int operator int() { return this->round(); } //assuming round is a Decimal member function
...
}
is that correct? you neglected to put a return type or any parameters in your example, so i'm not sure.


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  
Old 05-Apr-2005, 13:56
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
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  
Old 06-Apr-2005, 02:04
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Hey, I learned something new, too. You get rep points.
__________________
-Aaron
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 21:19.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.