Questions tagged [rule-of-zero]

Classes that have custom destructors, copy/move constructors or copy/move assignmentoperators should deal exclusively with ownership. Other classes should not have custom destructors, copy/move constructors or copy/move assignment operators.

To quote the original article:

Classes that have custom destructors, copy/move constructors or copy/move assignmentoperators should deal exclusively with ownership. Other classes should not have custom destructors, copy/move constructors or copy/move assignment operators.

The idiom was created by Martinho Fernandes in one of his blog-posts.

16 questions
22
votes
4 answers

Why does destructor disable generation of implicit move methods?

I was trying to understand what the rule of zero says by reading this blog. IMO, it says if you declare your own destructor then don't forget to make the move constructor and move assignment as default. Example: class Widget { public: ~Widget(); …
RaGa__M
  • 2,550
  • 1
  • 23
  • 44
15
votes
1 answer

Rule of Zero confusion?

So I have been reading about the Rule of Zero. Simplified version: I do not understand the purpose of this rule. The rule of three and five are sort of "rule of thumbs", but I cannot see the "rule of thumb" or any other specific intentions with…
user8270981
15
votes
1 answer

Does "The Rule of Zero" also apply for classes with virtual methods?

I find The rule of Zero as also mentioned on Peter Sommerlads Slides (p.32) very compelling. Although, I seem to remember that there was a strict rule that one has to define the destructor virtual, if the class has virtual members and is actually…
towi
  • 21,587
  • 28
  • 106
  • 187
8
votes
1 answer

unique_ptr, custom deleter, and Rule of Zero

I am writing a class that uses two objects created using a C interface. The objects look like: typedef struct... foo_t; foo_t* create_foo(int, double, whatever ); void delete_foo(foo_t* ); (similarly for bar_t). Because C++11, I want to wrap these…
Barry
  • 286,269
  • 29
  • 621
  • 977
8
votes
3 answers

C++ Rule of Zero : polymorphic deletion and unique_ptr behavior

In the recent overload journal under the topic Enforcing the rule of zero, the authors describe how we can avoid writing the Rule of five operators as the reasons for writing them are: Resource management Polymorphic deletion And both these can be…
Arun
  • 3,138
  • 4
  • 30
  • 41
7
votes
1 answer

C++ Rule of Zero & what is "user-declared" constructor?

Upon Lightness Races in Orbit's clarification, I've narrowed my post. After reading this article: The Rule of Zero, I came to understand the most, but I still want to solve some unclear issues that I have: 1. Looking at this phrase: If the…
Etay
  • 73
  • 7
6
votes
2 answers

rule of zero vs. base class destructors

I have a base class Base and a derived class D, and I'd like to have move constructor and move assignment operator automatically generated by the compiler for me. Following the Rule of Zero, I leave all memory management to the compiler and only use…
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
5
votes
1 answer

Understanding rule of zero

I have a base class, and I do not want to make derived class copyable. In order to make everything explicit I implement it in that way: class A { public: A() = default; …
galvanize
  • 537
  • 1
  • 5
  • 17
4
votes
1 answer

just add destructor that do nothing can cause compile error (around std::move), why?

While I was learning std::move, I found a strange issue. If I add only a destructor that do nothing to a perfect program, I will get a compile error. #include using namespace std; class M { public: int database = 0; M &operator=(M…
javaLover
  • 6,347
  • 2
  • 22
  • 67
3
votes
0 answers

How does rule-of-zero affect shared libraries with hidden visibility?

NOTE: The question is at the bottom. I'm trying to understand the problems that can occur if using rule-of-zero with shared libraries and derived types. In the demonstration below, DerivedType is compiled with rule-of-zero or not depending on a…
steveire
  • 10,694
  • 1
  • 37
  • 48
3
votes
1 answer

Rule of zero - default constructor not generated

I was reading this: https://en.cppreference.com/w/cpp/language/rule_of_three And my understanding from this is that, if you want to have a base class with a virtual destructor, the you need to define all 5 special functions (extracted from the rule…
code_fodder
  • 15,263
  • 17
  • 90
  • 167
3
votes
3 answers

Trouble understanding the C++11 syntax in the Rule of Zero

I am studying the Rule of Zero and have 2 questions for the final piece of code which demonstrates the rule. class module { public: explicit module(std::wstring const& name) : handle { ::LoadLibrary(name.c_str()), &::FreeLibrary…
goldcode
  • 633
  • 8
  • 21
2
votes
1 answer

How to properly apply rule of 5 (or zero?) to a class containing a vector of custom objects with strings

I'm having trouble wrapping my brain around ownership and maximizing performance with moves. Imagine this hypothetical set of classes emulating an Excel workbook. namespace Excel { class Cell { public: // ctors Cell() = default; …
Arthur Dent
  • 1,828
  • 13
  • 21
1
vote
1 answer

What is the difference between declaring a copy constructor with "= default" or not declaring it at all?

I am trying to understand the behaviour of auto-generated compiler code for various functions such as: destructor copy constructor assignment operator move constructor move assignment operator Will declaring them with "= default" cause any…
Yiğit
  • 55
  • 6
0
votes
0 answers

Implement C++ assignment operator in terms of constructor

Background Suppose you want to implement a resource-managing class in C++. You cannot use the Rule of Zero or Rule of Five Defaults, so you actually need to implement copy and move constructor, copy and move assignment operator, and destructor. In…
Daniel H
  • 7,223
  • 2
  • 26
  • 41
1
2