在基类崩溃中调用虚拟功能
Call to virtual function in base class crash
我在公共递归中呼叫一个受保护的虚拟功能的问题。
我想拥有的是 base class class class,它调用递归函数 getAllStrings(),然后将其称为 virtual getTring()在其"儿童"列表的所有项目上函数。
getTring()方法仅在其某些孩子类中实现。
//base.hpp
class Base
{
private:
// SharedBase is a typedef boost::shared_ptr<Base>
std::list<SharedBase> baseList;
protected:
virtual std::string getString() const
{
return "";
};
public:
Base(){}
virtual ~Base(){}
void addElmtToList(SharedBase sb)
{
baseList.push_back(sb);
}
// recursive
void getAllStrings(std::string &returnedString)
{
if (!baseList.empty())
{
// Iterate through all children
std::list<SharedBase>::iterator itBase = baseList.begin();
while (itBase != baseList.end())
{
(*itBase)->getAllStrings(returnedString);
++itBase;
}
}
returnedString += getString(); // ERROR !
}
};
//derived_a.hpp
class Derived_a : public Base
{
public:
Derived_a();
virtual ~Derived_a();
protected:
std::string getString() const
{
return "derived_a string";
}
};
//derived_b.hpp
class Derived_b : public Base
{
public:
Derived_b();
virtual ~Derived_b();
};
因此,基本上的应用程序呼叫 getallStrings(),除了调用 getString() ,使应用程序崩溃。
简单地调用 getString()函数,而无需任何递归率(注释 If block)使应用程序崩溃。
将其放置在非虚拟上使其正常工作,但是在这种情况下,功能是没有用的,因此我有点迷路了。为什么崩溃?
编辑10/03:在此处添加SSCCE。基于Synxis代码。在我的计算机上使用Boost Linked工作。所以我想这确实与我们的应用程序环境有关。
我终于解决了这个问题。
我用drmingw做了一个跟踪,显示在 ntdll.dll!RtlUlonglongByteSwap
但是,该函数中没有使用信号。
这是罪魁祸首:
//在" base.hpp"文件中
protected:
/// Some useful type definition for the signal to emit when the
/// configuration changes.
typedef boost::signals2::signal<void ()> OnConfigurationUpdated;
virtual std::string getString()
{
return "";
}
public:
/// Signal that subscribers have to connect to if they want to be alerted
/// about the configuration changes.
OnConfigurationUpdated reactOnChanges;
该项目使用另一个项目序列化,其中类别是导入类的地方。但是,该项目并未与GUI重建。从序列化项目中正确清洁(删除.o和.dll文件)使其有效。
相关文章:
- 如何在C++中获得"静态纯虚拟"功能?
- std::is_trivially_copyable_v 关于虚拟功能
- 隐藏重载虚拟功能的模板化访客:SFINAE 在使用?
- 为什么我无法覆盖虚拟功能?
- 虚拟功能不起作用
- 如果在创建对象时创建了 VPTR,那么为什么具有虚拟功能的类的大小在 32 位系统上为 4,在 64 位机器上为 8
- 虚拟功能如何影响static_cast?
- __declspec(属性)和虚拟功能
- 实现来自第三方类的虚拟功能
- 消除未使用的虚拟功能
- 为什么虚拟继承即使不涉及虚拟功能也需要 vtable?
- C 虚拟功能的输出
- 抽象类 A 由类 C 继承"through"类 B "intermediary"。如何声明虚拟功能?
- 虚拟功能是否不太可能导致堆栈溢出
- 使用继承和虚拟功能应用五个规则
- 两个派生类(十六进制 二进制)的虚拟功能
- 虚拟功能编译器优化C
- 虚拟功能指针指向派生类中的函数
- 隐藏的过载虚拟功能OSX QT4
- 使用虚拟功能在C 中进行猜测