Can I call a virtual function from a constructor?
来源:互联网 发布:unix 时间 java 编辑:程序博客网 时间:2024/05/16 03:21
Yes, but be careful. It may not do what you expect. In a constructor, the virtual call mechanism is disabled because overriding from derived classes hasn't yet happened. Objects are constructed from the base up, "base before derived".
Consider:
#include<string>#include<iostream>using namespace std;class B {public:B(const string& ss) { cout << "B constructor\n"; f(ss); }virtual void f(const string&) { cout << "B::f\n";}};class D : public B {public:D(const string & ss) :B(ss) { cout << "D constructor\n";}void f(const string& ss) { cout << "D::f\n"; s = ss; }private:string s;};int main(){D d("Hello");}
the program compiles and produce
B constructorB::fD constructorNote not D::f. Consider what would happen if the rule were different so that D::f() was called from B::B(): Because the constructor D::D() hadn't yet been run, D::f() would try to assign its argument to an uninitialized string s. The result would most likely be an immediate crash.
Destruction is done "derived class before base class", so virtual functions behave as in constructors: Only the local definitions are used - and no calls are made to overriding functions to avoid touching the (now destroyed) derived class part of the object.
For more details see D&E 13.2.4.2 or TC++PL3 15.4.3.
It has been suggested that this rule is an implementation artifact. It is not so. In fact, it would be noticeably easier to implement the unsafe rule of calling virtual functions from constructors exactly as from other functions. However, that would imply that no virtual function could be written to rely on invariants established by base classes. That would be a terrible mess.
- Can I call a virtual function from a constructor?
- How do I call a C++ function from C?
- Can a member function template be virtual?
- call a JavaScript function from C++ code
- Call Function in a ViewController from UITableViewCell
- can I make a cell phone call
- pure virtual function can has a function body
- How can a member function in my derived class call the same function from its base class?
- A const object can only call const function
- How can I fix a locale warning from perl?
- Constructor call must be the first statement in a constructor
- Constructor call must be the first statement in a constructor
- Constructor call must be the first statement in a constructor
- Constructor call must be the first statement in a constructor
- C++报错集合:cannot call member function “virtual void A:: initialize()”without object
- Can we call the Method of a controller from another controller in asp.net MVC?
- pure virtual function call
- How can I limit the CPU load and bandwidth when make a video call?
- Why don't we have virtual constructors?
- error LNK2019: 无法解析的外部符号__imp__ChooseFontA@4,该符号在函数 _ChooseNewFont@0 中被引用
- easyui $.message.alert 点击右上角的关闭按钮时,不执行定义的回调函数
- 深入到urb内部
- 基本TCP套接口编程
- Can I call a virtual function from a constructor?
- java_Web09-软件密码学基础和配置tomcat的https连接器和tomcat服务器的管理平台
- Hello, world!
- 进制转换 hdu2031 递归
- 用shell编写的简单密码加密程序,涉及到ASCII与字符的相互转换,以及命令结果输出到变量
- DB2游标原理
- 关于Cookie和Session
- 添加了新的类以后 重新打开工程 类视图不显示新的类 为什么? 在线等 求大神指教
- 学生管理系统(附设计要求)