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 define the default action in C++

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to define the default operation in C++", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to define the default operation in C++.

C.21: if you want to define the default operation, you can define it all, and if you want to prohibit it, you prohibit it all.

Reason (reason)

Special member functions include constructors, copy constructors, copy assignment operators, mobile constructors, mobile assignment operators, and destructors.

Translator's note: these functions all have the responsibility of managing the life cycle of data members, so implementation or prohibition needs to be unified.

The semantics of special functions are closely related, and if one needs to be declared, others may also need to be considered.

Defining all special functions except the default constructor, even in the form of = default or = delete, suppresses the implicit declaration of the mobile constructor and the mobile assignment operator. Declaring a mobile constructor or a mobile assignment operator, even in the form of = default or = delete, causes the implicitly generated copy constructor or copy assignment operator to be defined as = delete. Therefore, once any particular function is declared, others should be declared to avoid unnecessary effects. For example, turn all potential mobile operations into costly copy operations, or make the class mobile-only.

Example, bad (negative example)

Struct M2 {/ / bad: incomplete set of default operations

Public:

/ /...

/ /... No copy or move operations...

~ M2 () {delete [] rep;}

Private:

Pair* rep; / / zero-terminated set of pairs

}

Void use ()

{

M2 x

M2 y

/ /...

X = y; / / the default assignment

/ /...

}

Assuming that the destructor needs that "special mode" (in this case, freeing memory), the likelihood of copying and moving assignments (both implicitly destroying objects) is low.

Note (Note)

This is the well-known "5 special function rule" or "6 special function rule". The difference is whether the default constructor is included.

Note (Note)

If you need a default implementation of the default action (for example, other non-default ones are defined), use = default to indicate that you intend to do so. If you don't want the default action, generic = delete suppresses its generation.

Translator's note: for example, if some form of constructor is defined, the compiler will not generate the default constructor.

Example, good (sample)

If you need to declare a destructor, define it directly as virtual, which can be used as a default. To avoid suppressing implicit movement operations, they must also be declared. To prevent the class from becoming a move-only (and copy-prohibited) type, the copy operation must also declare:

Class AbstractBase {

Public:

Virtual ~ AbstractBase () = default

AbstractBase (const AbstractBase&) = default

AbstractBase& operator= (const AbstractBase&) = default

AbstractBase (AbstractBase&&) = default

AbstractBase& operator= (AbstractBase&&) = default

}

To avoid divergence due to rule C.67, copy and move operators can also be defined as deleted.

Class ClonableBase {

Public:

Virtual unique_ptr clone () const

Virtual ~ ClonableBase () = default

ClonableBase (const ClonableBase&) = delete

ClonableBase& operator= (const ClonableBase&) = delete

ClonableBase (ClonableBase&&) = delete

ClonableBase& operator= (ClonableBase&&) = delete

}

Defining only a move operation or a copy operation has the same effect, but the purpose should be clearly stated for each particular function to make it easier for the reader to understand.

Note (Note)

The compiler enforces most of this rule and ideally warns of any violation.

Note (Note)

Strongly opposes a class with a destructor that relies on implicitly generated copy operations.

Note (Note)

It is easy to make errors when writing 6 special member functions at the same time. Note the parameter types in the following code.

Class X {

Public:

/ /...

Virtual ~ X () = default; / / destructor (virtual if X is meant to be a base class)

X (const X &) = default; / / copy constructor

X & operator= (const X &) = default; / / copy assignment

X (X &) = default; / / move constructor

X & operator= (X &) = default; / / move assignment

}

Minor errors (such as spelling mistakes, missing const, using & instead of & &, or missing a special member function) can cause errors or warnings. In order to avoid boring code and possible errors, try to practice the "0 special function" principle.

Enforcement (implementation recommendations)

The (simple) class should either declare (even through = delete) all special functions, or none at all.

At this point, I believe you have a deeper understanding of "how to define the default operation in C++". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report