Re: Automatic and Dynamic Storage
V ha scritto:
class A {
public:
A() {
std::cout << "A()\n";
}
~A() {
std::cout << "~A()\n";
}
};
and:
class B {
public:
B() {
std::cout << "B()\n";
}
~B() {
std::cout << "~B()\n";
}
private:
A a;
};
(note that B has a data member "a" of type A).
Now, if I write:
B *ptr = new B();
the object of type B pointed by "ptr" has "dynamic storage duration",
or, in other words, is stored into the heap (Herb Sutter's talk of
"free store", but this is not the point of this question). Now, inside
B there is the data member "a", that has "automatic storage duration".
Stop! Who said that the data member ptr->a has automatic storage
duration? ptr->a is a sub-object of *ptr (the complete object) and 3.7.4
states precisely that: "The storage duration of member subobjects, base
class subobjects and array elements is that of their complete object
(1.8)." So in this particular case ptr->a has dynamic storage duration.
But if "a" has automatic storage duration, it's stored into the stack?
In my opinion no: rather, I think that is stored "somewhere else".
ptr->a is stored within the storage allocated for *ptr. How could it be
otherwise, being that the former is a subobject of the latter (see 1.8)?
So, quoting Herb Sutter, we can say that "the stack stores automatic
variables", but "not all automatic variables are stored into the
stack".
Question #1: is my deduction correct?
The premises are wrong so the deduction is ill-formed.
Question #2: if data member "a" is not stored into the stack, where
else can be stored? (Ok, this is an implementation-dependent question,
but I'm interested into the "ideas" behind this.)
It's not an implementation-dependent question! ptr->a must be stored as
a subobject of *ptr.
HTH,
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]