Re: Automatic and Dynamic Storage
On 14 mai, 16:37, V <vdu...@gmail.com> wrote:
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).
The free store is the name given in the standard, just like automatic
storage is the name to the stack. I think storage duration is a
different concept, linked to lifetime and not to the actual placement
of the objects.
Question #1: is my deduction correct?
The way you say it seems wrong.
There is no "a" variable. "a" is a member of the B object. It is part
of the B object.
The B object is on the free store, and that's it.
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.)
This is not implementation-dependent at all.
As I said, "a" is part of the B object, which is on the free store.
Hence "a" is on the free store.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]