I'm trying to call a constructor of 1 object with the constructors for other objects, and am having a problem with things deleting. The important bits of code look like the following:
class command {
public:
command(const string& s) {
x = (char*) calloc(s.size()+1, 1);
memcpy(x, s.c_str());
}
~command() {
free(x);
}
private:
char* x;
}
this constructor mallocs a char* which needs to be freed in the destructor.
class pair {
public:
pair(command comm1in, command comm2in)
:comm1(comm1in), comm2(comm2in){};
private:
command comm1;
command comm2;
}
this one tries to set its 2 fields to the commands sent in. I think it also calls their copy constructors (might be wrong though)
in another function, I call the constructor for pair like so pair p(command("something"), command("something else"));
unfortunately, this has the effect of calling the destructors on the just created values and then the copy constructor in the constructor for pair.
Is there a good way to initalize comm1 and comm2 without going through the copy/delete process? I think c++11 has rValue references and std::move but I don't know if those come into play here.
Aucun commentaire:
Enregistrer un commentaire