overload operator... a begginner info
-
struct a
{
double Val1;
double Val2;
double Val3;
}how do I overload the operator (+,-,*,/) and (+=,-=,*=,/=,=) I know that I will be something like
a a::operator + (a paramA1,a paramA2)
{
paramA1.Val1 += paramA2.Val1;
paramA1.Val1 += paramA2.Val1;
paramA1.Val1 += paramA2.Val1;
return paramA1;
}but for the X= (+=,-=...) I want it clean... and I was'nt able to find the ABC of overloading operator's the rules and the basis. thanks Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com
-
struct a
{
double Val1;
double Val2;
double Val3;
}how do I overload the operator (+,-,*,/) and (+=,-=,*=,/=,=) I know that I will be something like
a a::operator + (a paramA1,a paramA2)
{
paramA1.Val1 += paramA2.Val1;
paramA1.Val1 += paramA2.Val1;
paramA1.Val1 += paramA2.Val1;
return paramA1;
}but for the X= (+=,-=...) I want it clean... and I was'nt able to find the ABC of overloading operator's the rules and the basis. thanks Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com
I was'nt able to find the ABC of overloading operator's the rules and the basis. Where did you search? Tomasz Sowinski -- http://www.shooltz.com
-
I was'nt able to find the ABC of overloading operator's the rules and the basis. Where did you search? Tomasz Sowinski -- http://www.shooltz.com
in msdn... I've found that I can overload many operator, += << etc. and I cannot overload . :: ?: but no example, for a struct, a class, they said that we can define them as global but when I tried to do it with the = operator.. it didn't work (I've uderstand that I need "this->data"). So, what I want it's the philosophy, my overloaded operator work but I don't think it's the way to do that...
struct AStruct
{
double data[100];
AStruct &AStruct::operator= (AStruct paramA);
};It's a very beginner info I know but "overloading operator" I'm Ok, but do it well... it's may be an other thing Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com
-
in msdn... I've found that I can overload many operator, += << etc. and I cannot overload . :: ?: but no example, for a struct, a class, they said that we can define them as global but when I tried to do it with the = operator.. it didn't work (I've uderstand that I need "this->data"). So, what I want it's the philosophy, my overloaded operator work but I don't think it's the way to do that...
struct AStruct
{
double data[100];
AStruct &AStruct::operator= (AStruct paramA);
};It's a very beginner info I know but "overloading operator" I'm Ok, but do it well... it's may be an other thing Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com
You should read some good C++ book first - 'C++ Language' by Stroustrup or 'Effective C++' by Meyers. You should pass AStruct by const reference. It's much more effective than passing by value, which involves calling a copy constructor. This applies to all operators and 'normal' functions as well. Tomasz Sowinski -- http://www.shooltz.com
-
struct a
{
double Val1;
double Val2;
double Val3;
}how do I overload the operator (+,-,*,/) and (+=,-=,*=,/=,=) I know that I will be something like
a a::operator + (a paramA1,a paramA2)
{
paramA1.Val1 += paramA2.Val1;
paramA1.Val1 += paramA2.Val1;
paramA1.Val1 += paramA2.Val1;
return paramA1;
}but for the X= (+=,-=...) I want it clean... and I was'nt able to find the ABC of overloading operator's the rules and the basis. thanks Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com
For operators where the left argument is an "a" object, make the method a member of the class:
struct a
{
double d1, d2, d3;
a operator+ (const a& rhs) const;
};a a::operator+ ( const a& rhs ) const
{
a retval;retval.d1 = d1 + rhs.d1; // note "d1" is accessing this->d1
retval.d2 = d2 + rhs.d2;
retval.d3 = d3 + rhs.d3;return retval;
}For operators where the left argument is not an "a", you need to make the method global:
a operator+ (const double d, const a& rhs )
{
a retval;a.d1 = d + rhs.d1;
a.d2 = d + rhs.d2;
a.d3 = d + rhs.d3;return retval;
}Note that if you had a class, and the global operator+ needed to access protected/private members of "a", then the global func would have to be declared as a friend. --Mike-- http://home.inreach.com/mdunn/ This posting is provided "as was" with no warranties, guarantees, lotteries, or any of those little bags of peanuts you get on planes. You assume all risk for crossing the street without holding mommy's hand. © 2001 Mike's Classy Software. Member FDIC. If rash develops, discontinue use. :love: your :bob: with :vegemite: and :beer: