当前位置:网站首页>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;
}
}
边栏推荐
- Minimum dominating set (MDS) and its matlab code
- Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency
- redis版本怎么查看(查看redis进程)
- 新一代超安全蜂窝电池,思皓爱跑上市,13.99万起售
- UE4 common printing information methods for debugging
- 机器学习 | MATLAB实现RBF径向基神经网络newrbe参数设定
- 追踪伦敦银实时行情的方法
- 面试突击69:TCP 可靠吗?为什么?
- Error reporting: the network preview shows {xxx:['this field is required']}
- 散列表 ~
猜你喜欢

追踪伦敦银实时行情的方法

Meeting notification & meeting feedback & feedback details function of meeting OA project

Jupyter notebook中5个有趣的魔法命令

Api 接口优化的那些技巧

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

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

伦敦金即时行情带来什么机会?

Outlier detection and open set identification (2)

JWT token related configuration (global configuration identity authentication rewrites authenticate method)

What opportunities does the London gold real-time market bring?
随机推荐
MATLAB02:结构化编程和函数定义「建议收藏」
Meeting notification & meeting feedback & feedback details function of meeting OA project
Armeabi-v7a architecture (sv7a)
mysql存储过程 实现创建一张表(复制原表的结构新建的表)
B-tree~
AQS principle
DRF -- authentication, authority, frequency source code analysis, global exception handling, automatic generation of interface documents, RBAC introduction
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(6)开题答辩PPT
What opportunities does the London gold real-time market bring?
rk3399 9.0驱动添加Powser按键
mysql时间按小时格式化_mysql时间格式化,按时间段查询的MySQL语句[通俗易懂]
Andriod6.0 low power mode (turn off WiFi, Bluetooth, GPS, screen brightness, etc.)
【commons-lang3专题】003- RandomStringUtils 专题
时序预测 | MATLAB实现TCN时间卷积神经网络的时间序列预测
armeabi-v7a架构(sv7a)
新拟态个人引导页源码
状态压缩dp-蒙德里安的梦想
Have you seen the management area decoupling architecture? Can help customers solve big problems
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
散列表 ~