Linked Questions
1,850 questions linked to/from What is The Rule of Three?
51
votes
5
answers
84k
views
When do we need to define destructors? [duplicate]
I read that destructors need to be defined when we have pointer members and when we define a base class, but I am not sure if I completely understand. One of the things I am not sure about is whether ...
23
votes
9
answers
6k
views
Under what circumstances must I provide, assignment operator, copy constructor and destructor for my C++ class? [duplicate]
Say I've got a class where the sole data member is something like std::string or std::vector. Do I need to provide a Copy Constructor, Destructor and Assignment Operator?
3
votes
4
answers
10k
views
std::pair and class destructors [duplicate]
Possible Duplicate:
What is The Rule of Three?
How exactly does std::pair call destructors for its components? I am trying to add instances of a class to an std::map, but I am getting errors ...
6
votes
3
answers
6k
views
STL vector push_back() memory double free [duplicate]
Possible Duplicate:
What is The Rule of Three?
I have the a problem of the double freeing of memory in the following program.
The debugger shows that the issue is in the push_back() function.
...
11
votes
2
answers
8k
views
Double free or corruption when using destructor [duplicate]
In the following code when I add the the line which is specified with an arrow gives error:
Error in `./a.out': double free or corruption (fasttop):
0x00000000007a7030 * Aborted (core dumped)
The ...
5
votes
3
answers
4k
views
Why is my destructor being called and how can I fix it [duplicate]
I have issues with my destructor in my c++ program.When I run the program and take the users input, it suddenly calls the destructor before the cout can even print inside the statement. Assume that ...
1
vote
4
answers
2k
views
why are copy constructors needed and what are the cases where they are very helpful? [duplicate]
Why exactly do we need copy constructors?
I am studying C++ and I am not able to understand the need for copy constructors , as without using a copy constructor also, i was getting correct output.
I ...
3
votes
5
answers
2k
views
Copy Constructor in C++ [duplicate]
This is a general question that I've been asking for a while but couldn't get a clear answer to. Do I need to code a copy constructor for a class when all instance data fields in this class are ...
1
vote
5
answers
1k
views
double free or corruption (runs ok if reorder lines) [duplicate]
Use example in link, but change to use char * and vector:
#include <vector>
using namespace std;
class Test{
char *myArray;
public:
Test(){
myArray = new char[10];
}
~...
6
votes
1
answer
972
views
When to use overloaded assignment operator? [duplicate]
Possible Duplicate:
What is The Rule of Three?
When you require to define to your own assignment operator?
1
vote
3
answers
6k
views
why do we need a copy constructor when an array is allocated memory dynamically? [duplicate]
Possible Duplicate:
What is The Rule of Three?
Array(const Array &arraytoCopy)
:size(arraytoCopy.size)
{
ptr=new int[size];
for(i=0;i<size;i++)
ptr[i]=arraytoCopy.ptr[i];
}
...
0
votes
3
answers
2k
views
How to call the destructor of child class with a smart pointer of parent type? [duplicate]
In my application I used to have a unique_ptr<parent> _member as a member of a custom class. The code worked fine. However when I recently tried to create another class that inherit from the ...
6
votes
3
answers
387
views
Local variable deletes memory of another variable when going out of scope [duplicate]
While designing a class that dynamically allocates memory I ran into the following problem regarding memory allocation. I was hoping that some of you might be able to point me in the right direction ...
0
votes
1
answer
3k
views
C++ Why is my program getting the error free(): double free detected in tcache 2 in GDB [duplicate]
So I am having to do this exercise for a course in Udemy and I finished it. But running in on my own machine in GDB I get the error above in the title. I tried checking the values of the pointers for ...
0
votes
1
answer
1k
views
Calling a subclass method causes a segmentation fault [duplicate]
I'm facing a strange issue. I wrote a Parent abstract class (implementing a pure virtual test() method) and its Child class (implementing the test() method).
class Parent
{
public :
...