- In which sequence memory is allocated for data members('s' in this case).
Objects are always stored in a contiguous block of memory that cannot be resized. Therefore all sub-objects are allocated simultaneously when the super-object is allocated.
Note that an object like std::string may also allocate a dynamic memory buffer which is separate from the memory of the object itself. The earliest time this memory can be allocated is within the constructor of the string. That said, an empty string needs no buffer, so that allocation can be delayed until later.
- Does Object exist while calling fun() in the constructor.
No. The object doesn't exist completely yet. All of its memory has been before the constructor, and all of the sub objects will have been completely constructed before the constructor of the object. But the life time of an object starts when the constructor has finished. That said:
My doubt is how I am calling a function on the b object which is not yet created by the constructor.
It is fine to call member functions within the constructor.
You just have to make sure to not do anything that would rely on the constructor having been already executed, since it hasn't yet been executed. For example, if you specify a class invariant, that is established by the constructor, you then mustn't call any function that relies on that invariant before the invariant has been established.
Note that member functions can also be called from the member initialization list. There, some sub-objects have not been initialized yet, so great care must be taken to not call such member functions that would access the uninitialized sub-objects.
B() : s(fun()) {}stack, norheap. It's just a implementation detail, which, technically, can be different between compilers. 2) "Global data is created on the heap." What makes you to say so?std::stackand family), please, provide me with a chapter in a standard, where it is done so.