Hi,
The title makes it sound bad. I'll explain by example.
Sorry about the volume of code, but its simple.
template <class A>
class Pointer {
public:
Pointer()
tr(NULL) {};
Pointer() { if (ptr) delete ptr; };
void Add(T *pValue) {
ptr = pValue;
}
void Add(T &Value) {
Add(&Value);
}
private:
T* ptr;
};
class RF {
public:
RF():Age(0) {};
void SetAge(int Value) { Age = Value };
private:
int Age;
};
int main() {
RF Sue;
Sue.SetAge(21);
Pointer<RF> *P = new Pointer<RF>;
P->Add(Sue);
// .... do something
delete P;
return 0;
}
Finally, the question is. When I call delete P, which calls the Pointer destructor to delete ptr - do i have to distingush between variables that come from the stack or the heap ? will Sue be deleted twice ?
Thanks in advance.
Rich.
The title makes it sound bad. I'll explain by example.
Sorry about the volume of code, but its simple.
template <class A>
class Pointer {
public:
Pointer()
Pointer() { if (ptr) delete ptr; };
void Add(T *pValue) {
ptr = pValue;
}
void Add(T &Value) {
Add(&Value);
}
private:
T* ptr;
};
class RF {
public:
RF():Age(0) {};
void SetAge(int Value) { Age = Value };
private:
int Age;
};
int main() {
RF Sue;
Sue.SetAge(21);
Pointer<RF> *P = new Pointer<RF>;
P->Add(Sue);
// .... do something
delete P;
return 0;
}
Finally, the question is. When I call delete P, which calls the Pointer destructor to delete ptr - do i have to distingush between variables that come from the stack or the heap ? will Sue be deleted twice ?
Thanks in advance.
Rich.