开发者

Constructor and object intitiation

开发者 https://www.devze.com 2023-03-22 09:04 出处:网络
When we access \"this \\ Me\" in the constructor of any class, how is it that \"this\" is already available while its yet getting constructed? Has a temp开发者_如何学Goorary creation of the instance a

When we access "this \ Me" in the constructor of any class, how is it that "this" is already available while its yet getting constructed? Has a temp开发者_如何学Goorary creation of the instance already happened before the constructor call? If so then does this mean these Constructors are called after the actual object initialisation?


the object is created and the memory is allocated before you initialize it with the constructor.... ex 1. you create the object;

MyObject myObject;

2. you initialize it

myObject = new MyObject();

these 2 steps are also done when you are doing this:

MyObject myObject = new MyObject();

Edit: in the constructor this goes for myObject


In C++, when you have

Foo::Foo(int x)
: frob(x) {
    this->frob = x;
}

then the construction really happens exactly between then : and the first brace:

:<here>{

In the the body of that constructor, the object is fully constructed, therefore, using this there is well defined.

0

精彩评论

暂无评论...
验证码 换一张
取 消