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
1
vote
2 answers

Creating object with Copy Constructor (Simple Rule of Three Class) yields run-time error

I have the simple program below: #include class Counter { private: size_t m_count; public: Counter() : m_count(0) { std::cout << "defctor" << '\n'; } Counter(size_t count) : m_count(count) …
1
vote
1 answer

Is it really safe not to check in the copy assignemt operator whether an object is assigned to itself?

Here is an example implementing "Rule of Three" which I found: class Array { public: int size; int* vals; Array() : size(0), vals(NULL){} Array( int s, int* v ); Array(const Array&); // 1 Array&…
Maestro
  • 2,512
  • 9
  • 24
1
vote
2 answers

C++ - For a vector of pointers to an object, does reallocation cause deletion and copying of objects?

From my understanding so far, if you have a vector of class objects, if you erase any member of the vector, typically the vector will reallocate some of it's objects in order to preserve memory contiguousness. Hence you need to implement the rule of…
metamorphosis
  • 1,972
  • 16
  • 25
1
vote
3 answers

Default constructor missing - but I'm not calling it?

I'm writing a C++ application in which I have a Controller class with two nested structs, defined in my header file as follows: class Controller { struct help_message { // controller.hpp, line 19 std::string summary; …
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
1
vote
1 answer

vector does not erase content correctly (infite amount run of copy asignment operator untill crash [BEX])?

Well my problem is that after I want to "unload" loaded DLL's the copy assignmnent operator is called an unlimited amount of times until crash. The code from which I remove the vector data looks like this: void UnloadPlugins() { …
user1182183
0
votes
3 answers

Writing the Rule of Three for a 2D map in C++

I am making a program that determines if a tweet is happy or sad, and I was thinking I tokenize the tweets, then create a map that stores the word as the key, how many times it was used in total, and how many times it was used in a happy tweet and a…
user83975
  • 1
  • 2
0
votes
1 answer

Copy constructor of a class containing a smart pointer

In the following example we have a class Class that contains a Bridge object that takes care of all the memory handling for us (rule of three). class Base { public: Base() {}; virtual Base* clone() const = 0; virtual ~Base()…
Tyler D
  • 323
  • 1
  • 12
0
votes
1 answer

Hash table - issue with destructor (pointer being freed was not allocated)

I have a HashTable, where collisions are handled by chaining (linked lists). The first node of every linked list has a pointer from each array position. Shown below is a regular constructor along with rule of 3 functions. Although my code is…
510NH
  • 15
  • 4
0
votes
3 answers

"attempting to reference a deleted function" for copy constructor

I am trying to get my head around the Rule of 5. I have a class Renderable, which defines a custom destructor, so it seemed like a good candidate for the Rule of 5. This class creates some resources in its constructor, so my first thought was that I…
Dan
  • 1,198
  • 4
  • 17
  • 34
0
votes
1 answer

(c++) stl vector implemenation

I've implemented a simple vector-like structure It works well if i use vector or vector but when i use > it makes error Is there are good implementation code about vector stl or problem in my code? here is my code class…
martin
  • 1
  • 1
0
votes
2 answers

recursive call in copy constructor

I implemented a class following the rule of three, and I am getting a crash. Upon debugging I have came to the conclusion that the copy constructor is calling itself repeatedly instead of calling the equality operator. Why is this so? Shouldn't it…
0
votes
1 answer

Can anyone please give me an example of how to properly use "The Big Three" in C++?

Possible Duplicate: What is The Rule of Three? Hi, I've been reading about the topic, and many websites tell me about why do I need a ctor, copy ctor, and a dtor. But I have had trouble finding good examples on how to use them. Also I don't…
Jota
  • 234
  • 2
  • 11
0
votes
1 answer

C++ weird behaviour with stack variables and functions

I've got a String class with a char* buffer and a unsigned int length. The string class has two constructors: String(const char* str); String(const String& other); and a destructor ~String() which deletes the char array with delete[]…
psmith
  • 3
  • 1
0
votes
3 answers

c++ inheritance question

I have a question about this: class A { int a; int* pa; public: A(int i):a(i) , pa(new int(a)) { cout<<"A ctor"<
Avri
  • 13
  • 1
  • 3
0
votes
0 answers

Rule of 5 - Compiles without assignment operator

In an effort to understand the move operator when dealing with a member that is an array, I wrote the following development test. Why does this work and/or compile when I've violated the rule of 5 by not implementing an assignment or a move…
Christopher Pisz
  • 3,757
  • 4
  • 29
  • 65