当前位置:网站首页>Return the member function of *this
Return the member function of *this
2022-07-29 00:58:00 【GarryLau】
1. Return the difference between reference and non reference
Return reference example :
#include <iostream>
#include <string>
namespace test_starthis {
class Screen{
public:
Screen() = default;
Screen(int x, int y, std::string contents): x_(x), y_(y), contents_(contents), ct_(0){
std::cout << "Screen ctor with parameter called." << std::endl;
}
// In class implementation , Default inline
Screen& set(std::string new_contents){
contents_ = new_contents;
return *this;
}
Screen& move(int x, int y) {
x_ = x;
y_ = y;
return *this;
}
// from const The object returned by the function is const object , Therefore, the return value also needs to be added const
const Screen& display() const {
std::cout << "contents_: " << contents_ << std::endl;
std::cout << "x_: " << x_ << ", y_: " << y_ << std::endl;
++ct_; // Although the function is const, But because of ct_ By mutable Modify the , So you can modify ct_
std::cout << "The screen is displayed " << ct_ << " times." << std::endl;
return *this; // *this yes const object
}
private:
int x_ = 0;
int y_{
0};;
std::string contents_{
"my screen"};
mutable unsigned int ct_{
0}; // If you want to const Modify a data member of the class in the member function , You can add mutable keyword
};
auto main() -> int {
Screen screen;
screen.display();
screen.set("green screen").move(8,8).display();
screen.move(6,6).set("red screen").display();
//screen.display().set("error"); // [MinGW Makefiles] error: passing 'const test_starthis::Screen' as 'this' argument discards qualifiers [-fpermissive]
return 0;
}
}
Output :
contents_: my screen
x_: 0, y_: 0
The screen is displayed 1 times.
contents_: green screen
x_: 8, y_: 8
The screen is displayed 2 times.
contents_: red screen
x_: 6, y_: 6
The screen is displayed 3 times.
The above operations screen.set("green screen").move(8,8).display(); and screen.move(6,6).set("red screen").display(); All executed on the same object .
If you make move and set return Screen Instead of Screen& Then the behavior of the above two sentences will be very different . For simplicity, use a simpler example to illustrate .
#include <iostream>
#include <string>
namespace test_starthis {
class Screen{
public:
Screen() = default;
Screen(int x, int y, std::string contents): x_(x), y_(y), contents_(contents), ct_(0){
std::cout << "Screen ctor with parameter called." << std::endl;
}
// In class implementation , Default inline
Screen set(std::string new_contents){
contents_ = new_contents;
return *this;
}
Screen move(int x, int y) {
x_ = x;
y_ = y;
return *this;
}
// from const The object returned by the function is const object , Therefore, the return value also needs to be added const
const Screen& display() const {
std::cout << "contents_: " << contents_ << std::endl;
std::cout << "x_: " << x_ << ", y_: " << y_ << std::endl;
++ct_; // Although the function is const, But because of ct_ By mutable Modify the , So you can modify ct_
std::cout << "The screen is displayed " << ct_ << " times." << std::endl;
return *this; // *this yes const object
}
private:
int x_ = 0;
int y_{
0};;
std::string contents_{
"my screen"};
mutable unsigned int ct_{
0}; // If you want to const Modify a data member of the class in the member function , You can add mutable keyword
};
auto main() -> int {
Screen screen;
screen.display();
screen.move(6,6).set("red screen").display();
screen.display();
return 0;
}
}
Output :
contents_: my screen
x_: 0, y_: 0
The screen is displayed 1 times.
contents_: red screen // This is the content of the copy of the function return value
x_: 6, y_: 6 // This is the left side of the copy of the function return value
The screen is displayed 2 times. // This is a copy of the function return value display
contents_: my screen // From here we can see screen The content of the has not changed
x_: 6, y_: 6
The screen is displayed 2 times.
In this example screen.move(6,6).set("red screen").display(); amount to :
Screen tmp1 = screen.move(6,6);
Screen tmp2 = tmp1.set("red screen");
tmp2 = tmp2.display(); // because display Or reference , So there is no copy here
2. be based on const overloaded
#include <iostream>
#include <string>
namespace test_starthis {
class Screen{
public:
Screen() = default;
Screen(int x, int y, std::string contents): x_(x), y_(y), contents_(contents), ct_(0){
std::cout << "Screen ctor with parameter called." << std::endl;
}
// In class implementation , Default inline
Screen set(std::string new_contents){
contents_ = new_contents;
return *this;
}
Screen move(int x, int y) {
x_ = x;
y_ = y;
return *this;
}
// from const The object returned by the function is const object , Therefore, the return value also needs to be added const
const Screen& display() const {
std::cout << "const version -->\n";
do_display();
return *this;
}
Screen& display() {
std::cout << "non-const version -->\n";
do_display();
return *this;
}
void do_display() const {
std::cout << "contents_: " << contents_ << std::endl;
std::cout << "x_: " << x_ << ", y_: " << y_ << std::endl;
++ct_;
std::cout << "The screen is displayed " << ct_ << " times." << std::endl;
}
private:
int x_ = 0;
int y_{
0};;
std::string contents_{
"my screen"};
mutable unsigned int ct_{
0}; // If you want to const Modify a data member of the class in the member function , You can add mutable keyword
};
auto main() -> int {
Screen screen1;
screen1.display(); // Call not const edition
const Screen screen2(6, 6, "red screen");
screen2.display(); // call const edition
return 0;
}
}
Output :
non-const version -->
contents_: my screen
x_: 0, y_: 0
The screen is displayed 1 times.
Screen ctor with parameter called.
const version -->
contents_: red screen
x_: 6, y_: 6
The screen is displayed 1 times.
3. Class type forward declaration
#include <iostream>
#include <string>
namespace test_starthis {
class Controller;
class Screen{
public:
// Screen() = default; // It makes no sense , Because there are reference members , Must initialize... In the initialization list
Screen(Controller &controller): controller2_(controller){
// here Controller &controller Cannot be changed into Controller controller
std::cout << "Screen ctor with parameter called." << std::endl;
}
void setController(Controller); // forward declaration Of incomplete type Can be used for function parameters
Controller getController(); // It can also be used for function return value
private:
//Controller controller_; // error: field 'controller_' has incomplete type 'test_starthis::Controller'
Controller* controller1_; // [RIGHT], Pointer form can be used
Controller& controller2_; // [RIGHT], Reference form can be used , But note that the input parameter in the constructor must also be a reference , Because to create an object of a class, the class must be defined , Forward declarations are not definitions
};
auto main() -> int {
// complete Controller After the definition of
// Controller controller;
//Screen screen(controller);
return 0;
}
}
边栏推荐
- DDD领域驱动设计如何进行工程化落地
- Outlier detection and Gan network (1)
- Surfacecontrol and surfaceflinger communication
- 华为发布HarmonyOS 3.0,向“万物互联”再迈一步
- Android必备的面试技能(含面试题和学习资料)
- In the second round, 1000 okaleido tiger were sold out in one hour after logging in to binance NFT again
- 异步模式之工作线程
- 关于ThreadPool的一些注意事项
- 🧐 table1 | 一秒搞定你的三线表
- Minimum dominating set (MDS) and its matlab code
猜你喜欢

Outlier detection and open set identification (2)

第二轮1000个Okaleido Tiger,再次登录Binance NFT 1小时售罄

Relying on cloud business to support revenue growth alone, is Microsoft still overvalued?

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(6)开题答辩PPT

状态压缩dp-蒙德里安的梦想

Api 接口优化的那些技巧

DRF - paging, JWT introduction and principle, JWT quick use, JWT source code analysis, JWT custom return format, custom user issued token, custom token authentication class

分类预测 | MATLAB实现TCN时间卷积神经网络的时序分类预测

Consumer unit 消费单元

面试突击69:TCP 可靠吗?为什么?
随机推荐
【树莓派】widows电脑如何与树莓派连接
QT静态编译程序(Mingw编译)
【刷题笔记】链表内指定区间反转
机器学习 | MATLAB实现RBF径向基神经网络newrbe参数设定
Requestvideoframecallback() simple instance
Meeting notification & meeting feedback & feedback details function of meeting OA project
Execute immediate simple sample set (DML)
深度学习 | MATLAB实现TCN时间卷积神经网络spatialDropoutLayer参数描述
Relying on cloud business to support revenue growth alone, is Microsoft still overvalued?
Outlier detection and open set identification (2)
【commons-lang3专题】002- RandomUtils 专题
浅谈一下跨端技术方案
PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益
Several methods of multi-threaded sequential operation can be asked casually in the interview
zabbix部署及监控
主线程与守护线程
What are the methods to track the real-time market of London Silver?
Matlab02: structured programming and function definition "suggestions collection"
Breadth first search (BFS) and its matlab code
SQL Server 只有数据库文件,没有日志文件,恢复数据时报1813错误的解决方案