Hello everyone,
Even if I have tested with MSVC 2008 that no resource leak, I want to confirm with you whether it is good code to maintain resource and avoid resource leak. Also whether the code is dependent on some non-Spec regulated points, e.g. MSVC 2008 specific things.
The design pattern is, embed a sub-object into another object (e.g. embed Goo object g into Foo object), but in destructor of Foo, destructor of its sub-object (e.g. Goo object g) is not called explicitly.
I have tested destructor of Goo object g will be called in MSVC 2008, but I am not sure whether we could rely on this -- when object goes out of scope, its sub-object also goes out of scope and destructor always gets called?
thanks in advance,
George
Even if I have tested with MSVC 2008 that no resource leak, I want to confirm with you whether it is good code to maintain resource and avoid resource leak. Also whether the code is dependent on some non-Spec regulated points, e.g. MSVC 2008 specific things.
The design pattern is, embed a sub-object into another object (e.g. embed Goo object g into Foo object), but in destructor of Foo, destructor of its sub-object (e.g. Goo object g) is not called explicitly.
I have tested destructor of Goo object g will be called in MSVC 2008, but I am not sure whether we could rely on this -- when object goes out of scope, its sub-object also goes out of scope and destructor always gets called?
Code:
#include <iostream>
using namespace std;
class Goo {
public:
Goo()
{
cout << "constructing Goo" << endl;
}
virtual ~Goo()
{
cout << "destructing Goo " << endl;
}
};
class Foo {
public:
Goo g;
Foo (Goo _g) : g (_g)
{
cout << "constructing Foo " << endl;
}
virtual ~Foo()
{
cout << "destructing Foo " << endl;
}
};
int func()
{
Goo g;
Foo f (g);
return 0;
}
int main()
{
func();
return 0;
}
thanks in advance,
George