1. The function returns a reference to type
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
int a = 0; //数据成员
// 返回引用
A &f1() { return *this; }
// 返回非引用
A f2() { return *this; }
// a+=1
void combine() { a += 1; }
};
int main()
{
A x, y;
x.f1().combine();
//等价于
// A &temp1 = x.f1();
// temp1.combine();
y.f2().combine();
//等价于
// A temp2 = y.f2();
// temp2.combine();
cout << "x.a = " << x.a << endl;
cout << "y.a = " << y.a << endl;
return 0;
}
Function name | this type | * This type | Return Type |
---|---|---|---|
f1 | A *const | A | A& |
f2 | A *const | A | A |
analysis:
- f1 of the return type is A &, x.f1 () returns a reference to x, the action of x.f1 appropriate changes x.
- f2 of the return type is A, y.f2 () returns a copy of y, the operation of y.f2 () does not affect the y.
to sum up:
- Return * this member function can be combined in a single statement.
- For the return * this member function, if it is a reference return type, the return value is a reference to the class object; if it is not a reference return type, the return value is a copy of the class object.
2. const member functions
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
int a = 0; //数据成员
// 非常量成员函数
A &f1() { return *this; }
// 常量成员函数
const A &f2() const { return *this; }
};
int main()
{
A x1; //非常量
x1.f1();
x1.f2();
const A x2; // 常量
x2.f1(); //错误
x2.f2();
return 0;
}
Function name | this type | * This type | Return Type |
---|---|---|---|
f1 | A *const | A | A& |
f2 | const A *const | const A | const A& |
analysis:
- x1 is a variable, f2 is a constant member functions. When invoked x1 f2, this = & x1, this point const A, x1 is A, the object may point to point constant is the amount of the respective objects, x1.f2 () correctly.
- x2 is constant, f1 is not a constant member functions. When invoked x2 f1, this = & x2, this point A, x2 is const A, type mismatch, x2.f1 () error.
to sum up:
- Constant object, and the object of constant references and pointers can only call const member functions.
- Const object can call const member functions and non-const member functions. The compiler to call const member functions when overloaded.
3. const member function call sequence
#include <iostream>
using namespace std;
class A
{
public:
int a = 0; //数据成员
A &add()
{
a += 1;
return *this;
}
const A &show() const
{
cout << a << endl;
return *this;
}
};
int main()
{
// 先a+1, 再打印a
A a1;
a1.add(); //this=&a1, this指向A, a1是A
a1.show(); //this=&a1, this指向const A, a1是A
A a2;
a2.add().show();// a2.add()的结果不是常量,可以调用常量成员函数
// 先打印a, 再a+1
A a3;
a3.show(); //this=&a3, this指向const A, a3是A
a3.add(); //this=&a3, this指向A, a3是A
A a4;
a4.show().add(); // 错误,a4.show()的结果是常量。常量对象只能调用常量成员函数,add不是常量成员函数
return 0;
}
Function name | this type | * This type | Return Type |
---|---|---|---|
add | A *const | A | A & |
show | const A *const | const A | const A & |
analysis:
- a1 itself is not constant, it can call an ordinary member functions add member functions and constants show. a1.add () returns a reference to the binding of a1, this reference) had no effect on a1.show (.
A a1;
a1.add(); //this=&a1, this指向A, a1是A
a1.show(); //this=&a1, this指向const A, a1是A
//等价于
A &tem1 = a1.add();
a1.show();
- a2 itself is not constant, it can call an ordinary member function add. a2.add () returns a reference to the binding of a2, the reference is not a reference to the constant, it can call const member functions show.
A a2;
a2.add().show();
//等价于
A &temp2 = a2.add();
temp2.show();
- a3 itself is not constant, it can call const member functions show and ordinary member functions add. a3.show () returns a reference to the binding of a3, this reference has no effect on a3.add ().
A a3;
a3.show(); //this=&a3, this指向const A, a3是A
a3.add(); //this=&a3, this指向A, a3是A
//等价于
const A &temp3 = a3.show();
a3.add();
- a4 itself is not constant, it can call const member functions show. a4.show () returns a reference to the binding of a4, the reference is a reference to the constant, it can only call const member functions, can not be called ordinary member function add.
A a4;
a4.show().add(); // 错误
//等价于
const A &temp4 = a4.show();
temp4.add();
to sum up:
- Common member function combined with the function of the constant attention of the members when a statement is the order of their meanings may differ disassembled after their merger and such are the same meaning as a1 and a2, a3 and a4 different meanings.