Questions tagged [rule-of-three]

The rule of three (also known as the Law of The Big Three or The Big Three) is a rule of thumb in C++ that claims that if a class defines one of the following it should probably explicitly define all three: destructor, copy constructor, assignment operator

65 questions
3
votes
2 answers

What is a resource in C++?

In relation to this topic: What is the copy-and-swap idiom? It states that one class should at most handle one resource. What is meant by resource? EDIT: For example I have a class that handles the information for each monitor, and contains an array…
Built on Sin
  • 351
  • 2
  • 6
  • 19
3
votes
2 answers

Are there any static analysis tools that check for Rule of 3 (or Rule of 5 C++11)

I am currently working on a codebase that is built on a foundation of sand. There are numerous classes in supposedly tested libraries that violate the "Rule of 3". Most declare a non-trivial destructor, but are missing either a copy constructor or…
mark
  • 7,381
  • 5
  • 36
  • 61
3
votes
7 answers

Unusual destructor behaviour when copying over stack variables

I wrote a test to check whether destructors were called before an overwriting assignment on a stack variable, and I can't find any rational explanation for the results... This is my test (in Visual C++ 2008 Release mode): #include class C…
BinarySplit
  • 538
  • 1
  • 4
  • 8
3
votes
3 answers

Rule of Three in C++

I've read that The Rule of Three, What is The Rule of Three? is summarized as follows: If you need to explicitly declare either the destructor, copy constructor or copy assignment operator yourself, you probably need to explicitly declare…
squashed.bugaboo
  • 1,338
  • 2
  • 20
  • 36
2
votes
1 answer

What is the minimal class to extend for a no copy / no move type?

Let's say I want to create some classes to manage resources that shouldn't be copied nor moved, which would be the minimal class to extend and avoid mistakes? The idea is that by extending the class, I end on the safe side of the 0/3/5 rules. I have…
Josu Goñi
  • 1,178
  • 1
  • 10
  • 26
2
votes
3 answers

How do smart pointers affect the rule of 5?

I've learnt that when you use pointers in a class, you should implement the rule of 5. If you do not use pointers then you're okay, and in fact its preferable, to use the defaults. However, how does this work with smart pointers? For example a class…
CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106
2
votes
4 answers

Implementation of "Rule of Three" gone wrong

Below is an erroneous implementation of "The rule of three", which I try to understand. Debugging the program, I found that the debugger has a problem cleaning up int *k, which could be resolved either by defining int *k = nullptr or simply setting…
Imago
  • 521
  • 6
  • 29
2
votes
1 answer

Make 2 non-static fields (that are dynamic arrays) use memory near each other

Let B1 and B2 be a dynamic-size storage classes. (e.g. B1~std::vector B2~std::vector) In C++11, if I code B1 and B2's move and copy function (rule of five), a class C that contains them as fields will copy/move correctly by default…
cppBeginner
  • 1,114
  • 9
  • 27
1
vote
4 answers

Why is a non-default constructor NOT considered in the Rule of Three?

The rule of three (also known as the Law of The Big Three or The Big Three) is a rule of thumb in C++ that claims that if a class defines one of the following it should probably explicitly define all three: destructor, copy constructor, copy…
Eric Z
  • 14,327
  • 7
  • 45
  • 69
1
vote
2 answers

Reasons to have special copy assignment operator instead of simple destructor and in-place constructor

I have a class with own resource management: class Lol { private: // This is data which this class allocates char *mName = nullptr; public: Lol(std::string str) // In constructor just copy data from string { auto cstr =…
Vasilii Rogin
  • 157
  • 1
  • 9
1
vote
1 answer

C++ - clang tidy complain about rule of X?

This code raises a warning in clang tidy: Class 'Locker' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment…
LeXav
  • 63
  • 4
1
vote
2 answers

Linked List: How to implement Destructor, Copy Constructor, and Copy Assignment Operator?

This is my C++ code: #include using namespace std; typedef struct Node { int data; Node* next; }Node; class LinkedList { private: Node* first; Node* last; public: LinkedList() {first = last = NULL;}; …
Kevinkun
  • 21
  • 5
1
vote
0 answers

"pointer being freed was not allocated" while creating an array of template instantiations in C++

I will simplify the problem in order to present a minimal and reproducible example: I have a class Polynomial which is just a wrapper around an array of coefficients. For convenience in my analyses downstream it is a good idea to write it as a…
maciek
  • 1,807
  • 2
  • 18
  • 30
1
vote
2 answers

Allocating memory - Pointers

I want to know when or if I have to delete this object. Here is the constructor of a basic class Object Object.cpp: Objects::Objects{ Obj one = new Obj; Obj two = new Obj; } I know when allocating memory you are supposed to delete it at…
user12383896
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