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
2504
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?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
347
votes
9 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&…
Xeo
  • 129,499
  • 52
  • 291
  • 397
23
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:…
user7119460
  • 1,451
  • 10
  • 20
18
votes
3 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…
chris
  • 60,560
  • 13
  • 143
  • 205
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…
Sam Kauffman
  • 1,221
  • 11
  • 30
15
votes
2 answers

Is CppCoreGuidelines C.21 correct?

While reading the Bjarne Stroustrup's CoreCppGuidelines, I have found a guideline which contradicts my experience. The C.21 requires the following: If you define or =delete any default operation, define or =delete them all With the following…
alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51
10
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…
Michael Sync
  • 4,834
  • 10
  • 40
  • 58
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 *…
Rafael S. Calsaverini
  • 13,582
  • 19
  • 75
  • 132
6
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…
Elimination
  • 2,619
  • 4
  • 22
  • 38
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…
osgx
  • 90,338
  • 53
  • 357
  • 513
5
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…
chili
  • 665
  • 8
  • 20
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,…
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(); //…
Tony Park
  • 1,247
  • 1
  • 9
  • 14
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…
Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80
1
2 3 4 5