构造函数不能为虚函数的原因
2017-12-05 15:35
337 查看
I am one of the person who has pricked my head in understanding the above question. Let me explain in my way...
If you have virtual function or (let me take just "virtual" keyword as of now), it means that this member function can be redefined by derived class. When we have a virtual function in a class, it will have vtable (virtual table). This vtable will have the address of the virtual function which is defined in derived.
Now the point is, how you invoke the virtual function which is defined in derived class with the help of object of the class?
Once you create an object of class, vptr (virtual pointer) will be created. Which inturn will be pointing to the base address of the vtable. So now it is clear that you can invoke the virtual funtion of derived class only with an object that has created.
Now coming to virtual constructor,
If we make constructor as virtual in base, it means that it could be redefined in derived. Keep in mind that constructor is invoked during object creation (object is not created yet. still it is in the status "creating". Object will create only after executing constructor part code). Assume you are trying to create object of the class which has virtual constructor. During this process constructor of the class will be invoked. It looks for virtual keyword. Now it tries to look for virtual constructor in derived. But not possible bcz there is no vptr and no vtable avaibale at this point of time. So, when object is not created, then there is no vptr. If no vptr for this object, then how the consturtor of derived is invoked?. No address of this construtor will available in vtable.
Hence there is no point in having virtual constructor.
I believe that i have cleared your doubt.
If you have virtual function or (let me take just "virtual" keyword as of now), it means that this member function can be redefined by derived class. When we have a virtual function in a class, it will have vtable (virtual table). This vtable will have the address of the virtual function which is defined in derived.
Now the point is, how you invoke the virtual function which is defined in derived class with the help of object of the class?
Once you create an object of class, vptr (virtual pointer) will be created. Which inturn will be pointing to the base address of the vtable. So now it is clear that you can invoke the virtual funtion of derived class only with an object that has created.
Now coming to virtual constructor,
If we make constructor as virtual in base, it means that it could be redefined in derived. Keep in mind that constructor is invoked during object creation (object is not created yet. still it is in the status "creating". Object will create only after executing constructor part code). Assume you are trying to create object of the class which has virtual constructor. During this process constructor of the class will be invoked. It looks for virtual keyword. Now it tries to look for virtual constructor in derived. But not possible bcz there is no vptr and no vtable avaibale at this point of time. So, when object is not created, then there is no vptr. If no vptr for this object, then how the consturtor of derived is invoked?. No address of this construtor will available in vtable.
Hence there is no point in having virtual constructor.
I believe that i have cleared your doubt.
相关文章推荐
- 构造函数不能是虚函数的原因
- 构造函数不能声明为虚函数的原因及分析
- 构造函数不能是虚函数的原因
- 构造函数不能声明为虚函数的原因是:
- C++类的构造函数不能为虚函数的原因
- C/C++—— C++中构造函数不能是虚函数的原因分析
- C++类的构造函数不能为虚函数的原因
- 构造函数不能为虚函数的原因
- c++中构造函数不能为虚函数的原因
- 构造函数为什么不能是虚函数的原因
- 构造函数不能声明为虚函数的原因是:
- 构造函数为什么不能是虚函数
- 析构函数可以为virtual,构造函数则不能。原因?
- 为什么构造函数不能是虚函数
- 为什么构造函数不能是虚函数
- 构造函数为什么不能是虚函数 ( 转载自C/C++程序员之家)
- 构造函数不能是虚函数
- 构造函数为什么不能是虚函数
- 构造函数为什么不能是虚函数
- C++的构造函数为什么不能是虚函数