c++ - 了解调用堆栈

标签 c++ callstack

我有以下方法:

std::string MaterialLayer::getName()
{
    std::string idfMaterialName = this->material->getName() + std::string("-") +   cea::wstring2string(StringConverterHelper::toString((static_cast<double>((floor(this->thickness*1000)) / 10))));

    return idfMaterialName;
}

这是通过下面的一段代码调用的:

bsm::MaterialLayer * ml = this->o_bsm_material_layer;
std::string name = ml->getName();

当我在第二行(调用 ml->getName() 的地方)进行调试时,我输入了以下方法:

void Material::setName(const std::string &name)
{
    this->name = name;
}

但我不明白为什么调用它,因为被调用的方法是 Material 类的 setter,而原始调用是 MaterialLayer 类的 getter!

我指定:

  • 我已经重建了所有的解决方案
  • 全部以Debug方式编译
  • Visual Studio 是 2010 年
  • setName()的参数名是在调用瞬间,这导致后面抛出异常,本次调试事件的需要源于抛出的异常,为了理解为什么...

最佳答案

我能想象这样的事情会发生的唯一方法是用这样的例子:

class A
{
public:
  virtual void FuncA() ;
} ;


class B
{
public:
  virtual void FuncB() ;
} ;


void A::FuncA()
{
  printf("FuncA\n") ;
}

void B::FuncB()
{
  printf("FuncB\n") ;
}

int main()
{
  A a ;
  B *b ;

  b = (B*)&a ;

  a.FuncA();    // calls A::FuncA
  b->FuncB();   // b points actually to an A object
                // calling B::FuncB now actually calls A::FuncA

  return 0 ;
}

我想你的程序中也发生过类似的事情。

关于c++ - 了解调用堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27440892/

相关文章:

javascript - 为什么函数表达式不包含在执行上下文的变量对象中?

c++ - 在 C++ 中更喜欢命名空间或静态函数

c++ - 在 CUDA 中管理占用

c++ - 从OpenCV中的旋转图像旋转回点

.net - 如何获取FaultException的完整调用堆栈

memory-leaks - 如何从Valgrind获取完整的调用堆栈?

c++ - 如何使用包装类中定义的 __declspec(dllexport) 结构导出?

android - 为什么在 Android NDK 中不编译结构声明?

c++ - Visual Studio 的图形诊断未捕获任何数据

javascript - .defineProperty 方法期间调用堆栈和 this 的行为