Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C++;?_C++ - Fatal编程技术网

C++ 在C++;?

C++ 在C++;?,c++,C++,我有一个从std::exception派生的类a,还有一个从a派生的类B。我已经为A和B两个类重写了what()函数。我知道如果我有这样的拦网 catch(A& ob){ std::cout << ob.what(); } catch(A&ob){ std::cout发生这种情况的唯一方法是B::what()不重写基类中的函数,如果它的签名与基类中的函数不匹配,就会发生这种情况 请确保您没有忘记在B中设置const,以匹配基类中的const char*what()co

我有一个从
std::exception
派生的类
a
,还有一个从
a
派生的类
B
。我已经为
A
B
两个类重写了
what()
函数。我知道如果我有这样的拦网

catch(A& ob){
  std::cout << ob.what();
} 
catch(A&ob){

std::cout发生这种情况的唯一方法是
B::what()
不重写基类中的函数,如果它的签名与基类中的函数不匹配,就会发生这种情况


请确保您没有忘记在
B
中设置
const
,以匹配基类中的
const char*what()const

请发布一条。他可能只是忘记了
virtual
。我测试了它,效果很好。@Puppy,当它在
std::exception::what()
上时很难忘记它