Imagine, I have some class :
class MyClass{
public:
MyClass(unsigned int N): a(new double[N]){};
~MyClass(){delete[] a;};
private:
double* a;
}
In another class, i use a pointer to this class :
class MyOtherClass{
public:
MyOtherClass(unsigned int N):p(new MyClass(N)){};
~MyOtherClass(){delete p;};
MyClass* get_MyClass(){return p;};
private:
MyClass *p;
}
Then, in the main I need to get MyClass which is contained in MyOtherClass
int main(){
MyOtherClass moc(1e100);
MyClass mc;
mc <-> moc.get_MyClass();
}
the <->
is where I'm stuck. I want that mc
becomes what p points to but without copying the (huge) static array. Is there a way to do this conversion efficiently ?
Edit
thanks for your answers but i will precise something. as the time consuming part of the copy of MyClass comes from the copy of the static array, i thought i could do something like :
class MyClass{
public:
MyClass(unsigned int N): a(new double[N]),del(true){};
MyClass(MyClass* mc): a(mc.get_a()),del(true){};
~MyClass(){if(del){delete[] a;}};
double* get_a(){
del = false;
return a;
}
private:
double* a;
bool del;
}
with the main :
int main(){
MyOtherClass moc(1e100);
MyClass mc(moc.get_MyClass());
}
but i don't know if i will have memory leaks...