Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the special member function in C++ class

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to use special member functions in C++ class". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

1. Default constructor

Default constructors are constructors that can be initialized without parameters. There are two types of constructors: one with no parameters and one with default values for all parameters.

2. Copy constructor

Copy constructors are constructors that take objects of this class as arguments. Other parameters are possible, but default values must be provided.

3. Copy assignment operator

Overloading equals = assigns objects of this class to defined objects.

4. Destructor function

There's nothing to say.

5. Move constructor

C++11 added, the rvalue object of this class is the constructor of the parameter, and the rest is the same as copying the constructor.

6. Move Copy Operator

Same as copy assignment operator, except that argument is right value.

It is easy to confuse the definition, and the code will be clear:

#include #include class Foo {public: std::string s; //default constructor Foo() { std::cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report