code
#include <iostream>
using namespace std;
class C {
public:
int x;
C () {}
C(int a) : x(a) {}
// member function
C operator = (const C&);
};
C C::operator= (const C& param) {
x = param.x;
return *this;
}
int main()
{
C foo(1);
cout <<"foo.x = " << foo.x << endl;
C bar;
bar = foo;
cout <<"bar.x = " << bar.x << endl;
return 0;
}
run
foo.x = 1
bar.x = 1
ERROR
opeartor= must be a nonstatic member function
note
quote
Notice that some operators may be overloaded in two forms: either as a member function or as a non member function
many operators can be overloaded as member function or non member function
explain
The so-called member function is shown in the code section. There is a simple declaration about the operator to be overloaded in the class definition, such as:
C operator = (const C&);
Here the operator = (equal sign) is overloaded;
In contrast, non member function does not exist in the class definition. For example, the complete definition of a class consists of the following:
class D {
public:
int y;
D () {}
D (int b) : y(b) {}
};
There are many operators in C + +, such as = (equal sign), which can only be overloaded as member function. In other words, they must be declared in the class definition. See code;
At the same time, some operators, such as + (plus sign), can be overloaded as both member function and non member function.
doubt
The example code in the part of toturial [1] classes (II) / the keyword this that I read is as follows:
CVector& CVector::operator= (const CVector& param)
{
x=param.x;
y=param.y;
return *this;
}
Note that it is written as cvector & amp;
, referring to the class C
, then for = (equal sign), it should be written as C & amp;
, but in this way, the compiler (Dev C + + ISO C + + 11) will report an error and modify it to the final code before it can be compiled. This is the inconsistency between the current code and the example code.
reference
Classes (II)
http://www.cplusplus.com/doc/tutorial/templates/
Read More:
- Differences between length() size() and C strlen() of C + + string member functions
- error: invalid use of non-static member function
- C# Member XXX cannot be accessed with an instance with an instance reference;qualify it with a type
- error C4996: ‘scanf‘: This function or variable may be unsafe.Visual Studio Series compilers report errors using scanf function
- [Qt] error: call to non-static member function without an object argument
- C / C + + rounding function: round function
- C / C + + rounding function ceil(), floor ()
- non-member function ‘xxxx’ cannot have cv-qualifier
- call to member function bind_param() on boolean………..
- error C2784: ‘bool std::operator <(const std::_Tre
- Fatal error: Call to a member function bind_param() on a non-object in
- 【ERROR_2】Call to a member function bind_param() on a non-object
- PHP Fatal error: Call to a member function query() on a non-object in
- The use of C + + template function and lambda expression
- Call to a member function fetch_assoc() on a non-object
- How to use C + + function pointer array
- error C4996: ‘scanf’: This function or variable may be unsafe. Consider using scanf_s instead.
- C / C + + library function (tower / tower) realizes the conversion of letter case
- C + + programming fault handling — error: assignment of read only data member ‘STD:: pair
- Solution: regarding c2039, XXX is not a member of XXX