Operator Usage
-
Hi, I was wondering when using a operator such as "+" in order to be invoked does the code being executed have to be part of that class where the operator was defined or with regards for example a "+" operator if one the operands is of a type where the operator has been defined then the opertor code will get invoked. So..... CString + (LPSTR) invoked in any method of any class will execute the CString operator + code thankx
-
Hi, I was wondering when using a operator such as "+" in order to be invoked does the code being executed have to be part of that class where the operator was defined or with regards for example a "+" operator if one the operands is of a type where the operator has been defined then the opertor code will get invoked. So..... CString + (LPSTR) invoked in any method of any class will execute the CString operator + code thankx
hmmm... not certain I understand correctly, but ... If there's an operator defined for a class it will be called when it is referenced in the code; if the types are not allowable then the compiler will report an error (or warning). In you example, there is an operator that allow the
operator+
between a CString and a L(C)PSTR.friend CStringT operator+(
const CStringT& str1,
PCXSTR psz2
);Watched code never compiles.
-
Hi, I was wondering when using a operator such as "+" in order to be invoked does the code being executed have to be part of that class where the operator was defined or with regards for example a "+" operator if one the operands is of a type where the operator has been defined then the opertor code will get invoked. So..... CString + (LPSTR) invoked in any method of any class will execute the CString operator + code thankx
Normally when you overload an operator in a class, the left has side operand is taken as the caller. So assume you have
operator +
overloaded inclass A
. Consider the following code -Class A
{
public:
void operator +(int i) {}
};int main()
{
A Obj;
Obj + 3;
}Here the operator call is going to be converted to
Obj.operator+(3);
But if you write3 + Obj
it will fail because it becomes3.operator+(Obj)
which is illegal in C++. Because of this what is normally done is to create two global functions of the forms -void operator +(const A& obj, int i);
void operator +(int i, const A& obj);Then these 2 operators are declared as friend functions of the
class A
so that the functions can access all members of theclass A
.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
Hi, I was wondering when using a operator such as "+" in order to be invoked does the code being executed have to be part of that class where the operator was defined or with regards for example a "+" operator if one the operands is of a type where the operator has been defined then the opertor code will get invoked. So..... CString + (LPSTR) invoked in any method of any class will execute the CString operator + code thankx
To define an operator out side of a class, you place "friend" before the return type.
class A {
public:
A(int x) :num(x) {}
~A() {}
int GetVal() {return num;}
friend std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}
private:
int num;
};Which is the same as:
class A {
public:
A(int x) :num(x) {}
~A() {}
int GetVal() {return num;}
private:
int num;
};
std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}modified on Tuesday, April 20, 2010 5:55 PM
-
To define an operator out side of a class, you place "friend" before the return type.
class A {
public:
A(int x) :num(x) {}
~A() {}
int GetVal() {return num;}
friend std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}
private:
int num;
};Which is the same as:
class A {
public:
A(int x) :num(x) {}
~A() {}
int GetVal() {return num;}
private:
int num;
};
std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}modified on Tuesday, April 20, 2010 5:55 PM
Gwenio wrote:
To define an operator out side of a class, you place "friend" before the return type.
Only when the 'outside' (i.e. global namespace's) operator (like any other global namespace method) needs to access class
protected
orprivate
members.Gwenio wrote:
or you might do it like this (have not tried, but is should work): class A { public: A(int x) :num(x) {} ~A() {} int GetVal() {return num;} private: int num; }; friend std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}
Why do you think it works? I doesn't, of course. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Gwenio wrote:
To define an operator out side of a class, you place "friend" before the return type.
Only when the 'outside' (i.e. global namespace's) operator (like any other global namespace method) needs to access class
protected
orprivate
members.Gwenio wrote:
or you might do it like this (have not tried, but is should work): class A { public: A(int x) :num(x) {} ~A() {} int GetVal() {return num;} private: int num; }; friend std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}
Why do you think it works? I doesn't, of course. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
And so? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]