Ashley Durban SA
2010-10-04 04:22:29 UTC
1. void foo() {
2. Thing* tp1 = new Thing(1);
3. Thing* tp2 = new Thing(2);
4. Thing* tp3 = new Thing(3);
5. }
2.1
This function represents bad C++ practice because it creates memory leaks. Explain.
-
Say we make the Thing class inherit from QObject as follows:
class Thing : public QObject {
public:
Thing(int n, QObject* p = NULL)
: num(n) {
setParent(p);
}
// other member functions of Thing
private:
int num;
// other data members of Thing
};
Rewrite the foo() function so that it uses the child management facility inherited from QObject to prevent any memory leaks. (Note: The function should still declare at least three pointers to Things, and allocate heap memory to them.)