Re: Automatic and Dynamic Storage
On May 14, 8:09 pm, V <vdu...@gmail.com> wrote:
....
class B {
public:
B() {
std::cout << "B()\n";
throw std::invalid_argument("...");
}
~B() {
std::cout << "~B()\n";
}
private:
A a;
};
int main() {
try {
B *ptr = new B();
}
catch (...) {
// ...
}
}
(nota that the B's constructor throws an exception).
Now, since C++ destroys only fully constructed objects, ptr's
destructor will not be called. Nevertheless, I know that B::a's
destructor will be called (Am I wrong?)... Will be "automatically"
called... From which I wrongly concluded that B::a has an "automatic
storage duration"...
You are right in that B::a destructor will be called, and that's
exactly because B::a is fully constructed by the time control
reaches your throw statement, because a compiler transforms the
constructor more or less as follows:
// not valid C++
B::B() { ... } -> B::B() : a() { ... } ->
B::B() {
a.A();
try {
...
}
catch ( ... ) {
a.~A();
}
}
And then symmetric transformation is applied to the B's destructor.
The more appropriate word would be "implicitly" (instead of
"automatically").
--
Nikolai
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]