Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.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++ 派生对象不调用虚函数的原因 类基{ 公众: 虚空func2()常量{ 库特_C++ - Fatal编程技术网

C++ 派生对象不调用虚函数的原因 类基{ 公众: 虚空func2()常量{ 库特

C++ 派生对象不调用虚函数的原因 类基{ 公众: 虚空func2()常量{ 库特,c++,C++,即复制对象,它不是原始派生对象,而是从派生对象初始化的基础实例副本 func1(Base&temp)应该可以工作这是因为您通过值传递一个参数:func1参数是Base temp,而不是Base&temp。因此,您构造了一个新的、临时的类对象Base,因此您可以调用打印1的Base::func2()。您应该尝试: func1(Base temp) virtualvoid func1(基本和临时){ 无法尝试通过引用将参数传递给func1,例如:void func1(Base&temp)。还要查看

即复制对象,它不是原始派生对象,而是从派生对象初始化的基础实例副本


func1(Base&temp)
应该可以工作

这是因为您通过值传递一个参数:
func1
参数是
Base temp
,而不是
Base&temp
。因此,您构造了一个新的、临时的类对象
Base
,因此您可以调用打印1的
Base::func2()

您应该尝试:

func1(Base temp)
virtualvoid func1(基本和临时){

无法尝试通过引用将参数传递给
func1
,例如:
void func1(Base&temp)
。还要查看@Wintermute的注释以了解更多细节。
func1(Base temp)
virtual void func1(Base& temp){
        cout << "Class 1" << endl;
        temp.func2();
    }