0

In the following code , I have created a class named ele, and I'm trying to store ele objects in a vector v. I am using deep copy by delegating copy constructor to the constructor with integer as an argument. I am getting an unusual error , a header file allocator.h opens up in my IDE(devC++) when I try to run it and I don't know what's going wrong.
If I comment out the copy constructor, the program runs with shallow copying without any compiler errors ( however, that is not what I want to do )

#include <iostream>
#include <vector>
using namespace std;
class ele{
    public:
    int* data_ptr;

    ele(int a) {
        data_ptr=new int;
        *data_ptr=a; 
        cout<<"new ele created with data="<<*data_ptr<<endl;
    }
    ele(ele &s):ele(*s.data_ptr) {
        cout<<"object with data="<<*data_ptr<<" copied"<<endl;
    }
    ~ele(){ 
        cout<<*data_ptr<<"destroyed"<<endl; 
        delete data_ptr; 
    }
};
void display(ele a){
    cout<<*a.data_ptr<<endl;
}
ele create(int k){
    ele* a=new ele(k);
    return *a;
}
int main(){
    vector <ele> v;
    int t=10;
    while(--t)
    {
        v.push_back(create(t));
    }
}
anonymous38653
  • 393
  • 4
  • 16
  • what is the error message? Please copy-paste it. – bolov Apr 27 '20 at 00:13
  • @bolov I have a copy construtor `ele(ele &s)` and I didn't create default constructor as I didn't need it for this program. – anonymous38653 Apr 27 '20 at 00:23
  • Your destructor has undefined behaviour since it prints `*data_ptr` immediately AFTER `delete *data_ptr`. Unrelated to that - look up "rule of three" or (C++11 and later) "rule of five" for more info about things you're doing wrong - which also cause undefined behaviour with a class that is used as elements of a vector. – Peter Apr 27 '20 at 00:38
  • @Peter, thanks for pointing that out. I've edited and changed the order of statements. – anonymous38653 Apr 27 '20 at 00:43

1 Answers1

1

This is because your copy constructor should take a const ele &

ele(const ele &s):ele(*s.data_ptr) {
    cout<<"object with data="<<*data_ptr<<" copied"<<endl;
}
Mickael B.
  • 4,755
  • 4
  • 24
  • 48
  • yes , that's working . Can you elucidate how that helped ? – anonymous38653 Apr 27 '20 at 00:28
  • @AjayTyagiB18ME003 If you don't use a reference to const it's not a proper copy constructor because it can modify the argument passed. – eesiraed Apr 27 '20 at 00:33
  • @BessieTheCow That's right, but since no statement in the body of the constructor modifies the argument, then how does it get modified by itself ? – anonymous38653 Apr 27 '20 at 00:39
  • @AjayTyagiB18ME003 The compiler might not have access to the definition of the function when it is compiling the code that calls the function, so it is assumed that something can be modified if it's not explicitly marked const. – eesiraed Apr 27 '20 at 00:44
  • @AjayTyagiB18ME003 If you write `const ele obj1; ele obj2(obj1);` If won't work if your copy constuctor's parameter is not a const. Also see https://stackoverflow.com/questions/16956693/why-c-copy-constructor-must-use-const-object and http://www.geeksforgeeks.org/copy-constructor-argument-const/ – Mickael B. Apr 27 '20 at 00:49