Question

I want to know what is the process of creating an object of user defined type.

Example

class A{
    int a; 
    void f();
    int b;
    A() /*initialization list*/ {}
};

int main(){
    A ob;
}

1) I want to know at what time the object gets allocated, and at what time the constructor of A will get called.
2) When does its member get allocated? Is it at the same time the object itself gets allocated ?
3) When can I say that the object is fully created and safe to use? Is it just after the beginning of the constructor, or until the end of constructor?
4) I know that members get initialized in the constructor initialization list, so it's before the call of the constructor. Does that mean the members exist before the existence of the object?

5) When does the object get deallocated? Is it before the destructor ends?

Was it helpful?

Solution

  1. At the line "A ob;". At first storage for object is allocated, then the constructor is called.
  2. It's when right before the object constructor call.
  3. If the constructor didn't finished it's work - object isn't created yet. If you got an exception in the constructor, destructor won't be called.
  4. Yes. Your object doesn't exist, but is allocated.
  5. Object is deallocated after destructor call (or without it's call if there was an exception in constructor).

Some additional info

OTHER TIPS

The object is created when you enter the scope where it is defined.

It first allocate the object, and then call its constructor. You object is safe to use within the end of the constructor execution. Allocating the object reserve the sizeof(A), so each member is allocated at the same time (thats why those are called members).

The intialization list is called before the constructor is executed, but at this time, it is already allocated, therefore its safe to initialize them, because your object exists.

The object gets dealocated whithin the end of the destructor execution, not before.

1) On your A ob; line but it will be allocated on the stack, not on the heap!

2) So the constructor will be called, so yes.

3) If you use default constructor, yes you can do

4) Looks like the same question as 3, so yes

5) No, you can access members in the destructor

  1. First, the memory for an instance of class A is allocated, that includes memory occupied by its members. Afterwards, the constructor A::A() is called.
  2. Yes, see above.
  3. The answer depends on your definition of "fully created". It is safe to do almost anything in the body of constructor, with some caveats. The most known is about virtual method calls.
  4. Constructor initialization list is a part of the constructor, it just executes before control flow reaches the opening brace of the constructor.
  5. On destruction, the destructor A::~A() is called first, then the memory occupied by the object is deallocated.

1. The object will be allocated when the object constructor is called and the object constructor will be called when you create an object of the class.
2.
3.At the end of the constructor because all members must be initialized
4.
5.After the destructor is called.

At first, you should declare constructor into public section your class.

  1. Memory allocation occurs when a object is created (For you, it allocated on stack)
  2. Members get allocated, when the object is creating
  3. After the object is created (In your case, A ob)
  4. No, members initialise at moment constructor creating
  5. At first call destructor, then deallocation

Note: standard с++ does not specify the order of initialization of static members. It depends on the compiler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top