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
Questions tagged [rule-of-three]
58 questions
2284
votes
8 answers
What is The Rule of Three?
What does copying an object mean?
What are the copy constructor and the copy assignment operator?
When do I need to declare them myself?
How can I prevent my objects from being copied?
![](../../users/profiles/252000.webp)
fredoverflow
- 237,063
- 85
- 359
- 638
336
votes
8 answers
Rule-of-Three becomes Rule-of-Five with C++11?
So, after watching this wonderful lecture on rvalue references, I thought that every class would benefit of such a "move constructor", template MyClass(T&& other) edit and of course a "move assignment operator", template MyClass&…
![](../../users/profiles/500104.webp)
Xeo
- 123,374
- 44
- 277
- 381
17
votes
3 answers
Must a c++ interface obey the rule of five?
What is the correct way to declare instantiation methods when defining an interface class?
Abstract base classes are required to have a virtual destructor for obvious reasons. However, the following compilation warning is then given:…
![](../../users/profiles/7119460.webp)
user7119460
- 1,131
- 6
- 17
16
votes
3 answers
Exception to the Rule of Three?
I've read a lot about the C++ Rule of Three. Many people swear by it. But when the rule is stated, it almost always includes a word like "usually," "likely," or "probably," indicating that there are exceptions. I haven't seen much discussion of what…
![](../../users/profiles/2180479.webp)
Sam Kauffman
- 1,073
- 11
- 30
15
votes
2 answers
Understanding -Weffc++
Consider the following program:
#include
struct S {
S (){}
private:
void *ptr = nullptr;
std::string str = "";
};
int main(){}
This, when compiled with -Weffc++ on GCC 4.7.1, will spit out:
warning: 'struct S' has pointer…
![](../../users/profiles/962089.webp)
chris
- 55,166
- 13
- 130
- 185
9
votes
3 answers
Safe assignment and copy-and-swap idiom
I'm learning c++ and I recently learned (here in stack overflow) about the copy-and-swap idiom and I have a few questions about it. So, suppose I have the following class using a copy-and-swap idiom, just for example:
class Foo {
private:
int *…
![](../../users/profiles/114388.webp)
Rafael S. Calsaverini
- 12,352
- 16
- 69
- 126
9
votes
5 answers
C++ Copy Constructor + Pointer Object
I'm trying to learn "big three" in C++.. I managed to do very simple program for "big three".. but I'm not sure how to use the object pointer.. The following is my first attempt.
I have a doubt when I was writing this...
Questions
Is this the…
![](../../users/profiles/217593.webp)
Michael Sync
- 4,454
- 8
- 35
- 53
6
votes
2 answers
What's with the copy-constructor if the class contains a user-declared destructor?
The Standard in section 12.8/7 says:
If the class definition does not explicitly declare a copy
constructor, one is declared implicitly. If the class definition
declares a move constructor or move assignment operator, the
implicitly declared…
user2953119
6
votes
3 answers
Storing objects in STL vector - minimal set of methods
What is "minimal framework" (necessary methods) of complex object (with explicitly malloced internal data), which I want to store in STL container, e.g. ?
For my assumptions (example of complex object Doit):
#include
#include…
![](../../users/profiles/196561.webp)
osgx
- 80,853
- 42
- 303
- 470
5
votes
1 answer
rule of five and implicitly deleted functions
For my understanding, the rule of five is a guidelince rule. Altough, I've seen that the compiler in some scenarios may delete functions, implicitly. For example, when defining a move-ctor', the copy assignment/ copy ctor' will be deleted.
I'd like…
![](../../users/profiles/3880275.webp)
Elimination
- 2,245
- 2
- 14
- 32
4
votes
2 answers
Do C++ abstract classes need to obey the rule of five?
When implementing an abstract class like this:
class Base
{
public:
virtual ~Base() = default;
virtual void foo() = 0;
};
Does this interface have to obey the rule of five i.e. do I have to add a copy constructor, copy assignment operator,…
![](../../users/profiles/4700553.webp)
Markus Rothe
- 43
- 3
4
votes
1 answer
Rule of 3 Default Member Deprecation in C++11
According to the below widely-known table, automatic compiler generation of default copy constructor and copy assignment is deprecated in C++11 when one or more of the copy assignment, copy constructor, and destructor is/are supplied by the user…
![](../../users/profiles/3773305.webp)
chili
- 581
- 5
- 18
4
votes
5 answers
When assigning in C++, does the object we assigned over get destructed?
Does the following code fragment leak? If not, where do the two objects which are constructed in foobar() get destructed?
class B
{
int* mpI;
public:
B() { mpI = new int; }
~B() { delete mpI; }
};
void foobar()
{
B b;
b = B(); //…
![](../../users/profiles/500690.webp)
Tony Park
- 1,139
- 8
- 14
4
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…
![](../../users/profiles/926751.webp)
mark
- 6,939
- 5
- 34
- 54
3
votes
5 answers
Am I violating Rule of three?
I recently read, Rule of three and am wondering if I am violating it?
In my GUI application, classes like MainFrame, Interface, Circuit, Breadboard etc. (class name are indicative) have a single instance of each of them. In their constructors, I…
![](../../users/profiles/558094.webp)
Vinayak Garg
- 6,228
- 10
- 49
- 78