如何防止在构造函数或析构函数中调用虚函数?

发布于 2024-10-08 05:51:39 字数 661 浏览 2 评论 0原文

一些 C++ 材料提到我们不能在 ctor 或 dtor 中调用虚函数,

(
抱歉,我认为最好更改为
一些 C++ 材料提到我们最好不要在 ctor 或 dtor 中调用虚函数,

但我们可能会意外调用它们。有什么办法可以防止这种情况发生吗?

例如:

# include < iostream >  
using namespace std;  

class CAT  
{  
public:  
    CAT(){ f();}  
    virtual void f(){cout<<"void CAT:f()"<<std::endl;}  
};  

class SMALLCAT :public CAT  
{  
public:  
    SMALLCAT():CAT()  
    {  
    }  
    void f(){cout<<"void SMALLCAT:f()"<<std::endl;}    
};    

int main()  
{  
    SMALLCAT sc;   

}  

输出:

void CAT::f()  //not we expected!!!

谢谢

Some C++ materials mention we can't call a virtual function inside ctor or dtor,

(
sorry ,I think it's better to change to
Some C++ materials mention we'd better not to call a virtual function inside ctor or dtor,

)

but we may call them accidentally. Is there any way to prevent that?

For example:

# include < iostream >  
using namespace std;  

class CAT  
{  
public:  
    CAT(){ f();}  
    virtual void f(){cout<<"void CAT:f()"<<std::endl;}  
};  

class SMALLCAT :public CAT  
{  
public:  
    SMALLCAT():CAT()  
    {  
    }  
    void f(){cout<<"void SMALLCAT:f()"<<std::endl;}    
};    

int main()  
{  
    SMALLCAT sc;   

}  

output:

void CAT::f()  //not we expected!!!

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女的英雄梦 2024-10-15 05:51:39

你需要把那些“C++材料”扔进垃圾桶。

您当然可以从构造函数或析构函数调用虚拟函数。他们会做好自己的工作。您只需要了解语言规范,该规范明确规定虚拟调度机制根据对象的当前动态类型工作,而不是按照其最终预期动态类型工作。这些类型对于正在构造/析构的对象来说是不同的,当新手尝试从构造函数/析构函数调用虚函数时,这常常会让他们感到困惑。尽管如此,从构造函数和析构函数调用虚函数是该语言的有用功能,只要您知道它们的作用以及它们在这种情况下如何工作即可。为什么要“阻止”它?

这就像说除法运算符是危险的,因为可以将某个值除以零,并询问如何“阻止”它在程序中使用。

You need to throw those "C++ materials" to the garbage bin.

You certainly can call virtual functions from the constructor or destructor. And they will do their job. You simply need to be aware of the language specification that clearly states that virtual dispatch mechanism works in accordance with the current dynamic type of the object, not with its final intended dynamic type. These types are not the same for an object under construction/destruction, which often confuses newbies when they attempt to invoke virtual functions from constructors/destructors. Nevertheless, calling virtual functions from constructor and destructor is useful feature of the language, as long as you know what they do and how they work in such cases. Why would you want to "prevent" it?

It is like saying that division operator is dangerous since one can divide something by zero, and asking how to "prevent" its use in the program.

渡你暖光 2024-10-15 05:51:39

您可以在析构函数内调用虚拟函数。仅在某些情况下它不起作用,并且可能会使您的程序崩溃。避免打电话给他们的方法就是不打电话给他们。据我所知,除了可能使用一些静态分析工具来查看代码并警告您此类潜在问题之外,没有什么比这更好的了。

You can call a virtual function inside a destructor. Only in certain cases it won't work, and may crash your program. The way to avoid calling them is to not call them. There's nothing fancier I know of, other than possibly some static analysis tools to look at your code and warn you of potential problems like this.

余罪 2024-10-15 05:51:39

您当然可以在 ctor/dtor 内调用虚拟函数。问题是你的虚函数表是在每个构造函数(和析构函数)中设置的,因此你的虚函数调用将调用当前正在设置的类的实现。如果这就是你想要的,那就太好了。但是,您也可以节省 vtable 查找并执行作用域函数调用。

You certainly CAN call a virtual function inside a ctor/dtor. The issue is that your vtable is set up in each constructor (and destructor), so your virtual function call will call the implementation of the class that is currently being set up. If that's what you want, cool. But then you may as well save yourself the vtable lookup and do a scoped function call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文