Questions tagged [rule-of-five]

20 questions
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
3
votes
0 answers

C++ PIMPL using std::unique_ptr and rule of five

If having a c++ class with a pimpl using std::unique_ptr and solving the fact that the pimpl class is incomplete in the header by declaring my own destructor (I know i could also provide a custom deleter, but lets go with the dtor for this one). If…
joaerl
  • 1,012
  • 1
  • 10
  • 21
3
votes
0 answers

Base class virtual destructor - rule of five?

I have a base State interface class with virtual default destructor. class State { public: virtual void event() = 0; virtual ~State() = default; // relevant part virtual void onCreate() {} virtual void onDestroy() {} virtual…
weno
  • 804
  • 8
  • 17
2
votes
1 answer

Do I need to respect the rule of five here?

So on https://en.cppreference.com/w/cpp/language/rule_of_three it says: Because the presence of a user-defined (or = default or = delete declared) destructor, copy-constructor, or copy-assignment operator prevents implicit definition of the move…
lvalue
  • 51
  • 3
2
votes
2 answers

How should I deal with the destructor, when making a custom vector with a template class?

I tried to make my custom Vector class, with a template class. I expect I can put my Vector into a Vector> variable. At least that was what I was hoping for... but it keeps crashing at the destructor code. Here's my code. #include…
Hashnut
  • 367
  • 3
  • 18
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
1 answer

Why is a class with an explicitly declared move constructor failing `std::is_move_constructible_v`?

A library I'm using requires that classes used with its templates are move constructible and assignable, for various internal reasons. I've written a class, and given it an explicit move constructor. #pragma once #include namespace ECS…
Stefan Bauer
  • 393
  • 1
  • 9
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
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
0 answers

C++ Rule of 5 with inheritance implementation

Following the examples in the online tutorials about the rules of 5, I wrote this class: #include #include #include class A2 { char* _buff; public: A2(const char *text = "test") { std::cout << "New…
Andrea
  • 55
  • 7
1
vote
0 answers

Rule of five when using polymorphism

I'm trying to get my head around the rule of five when using interfaces(the concept anyway) and abstract classes and struggling to understand how the rules work. Suppose I have a layout like this: #include #include #include…
incubus
  • 681
  • 1
  • 5
  • 21
1
vote
2 answers

Defining interfaces (abstract classes without members) in C++

By an interface (C# terminology) I mean an abstract class with no data members. Thus, such a class only specifies a contract (a set of methods) that sub-classes must implement. My question is: How to implement such a class correctly in modern…
Adomas Baliuka
  • 1,384
  • 2
  • 14
  • 29
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
1
vote
2 answers

std::vector with elements allocated on the heap - do I need rule of 5?

If I have a class with members like this: class MyClass { public: void set_my_vector() { for (int ind = 0; ind < 3; ++ind) { my_vector.push_back(new MyStruct(i, i*2)); } } private: struct MyStruct { …
user_185051
  • 426
  • 5
  • 19
1
2