为什么虚函数需要通过指针而不是值(对象)传递?

Why do virtual functions need to be passed with a pointer and not by value(of the object)?(为什么虚函数需要通过指针而不是值(对象)传递?)
本文介绍了为什么虚函数需要通过指针而不是值(对象)传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我理解虚方法和 vtable 的概念,但我不明白为什么将对象作为指针(或引用)传递和按值传递(哪种废弃 vtable 或什么?)

I think I understand the concept of virtual methods and vtables, but I don't understand why there is a difference between passing the object as a pointer(or reference) and passing it by value (which kind of scraps the vtable or something?)

为什么会有这样的工作:

Why would something like this work:

Material* m = new Texture;
poly->setMaterial(m); 
// methods from Texture are called if I keep carrying the pointer around

不是这个?:

Material m = Texture();
poly->setMaterial(m);
// methods from Material are called if I pass the value around

推荐答案

因为如果按值传递,那么 会发生对象切片,无法实现运行时多态.在您的代码中,Material m = Texture() 行会导致对象切片.所以即使你通过指针(或引用)传递m,也无法实现运行时多态性.

Because if you pass by value, then object slicing will occur, and runtime polymorphism cannot be achieved. And in your code, the very line Material m = Texture() causes object slicing. So even if you pass m by pointer (or reference), runtime polymorphism cannot be achieved.

另外,运行时多态性是通过以下方式实现的:

Also, runtime polymorphism is achieved through:

  • 基本类型的指针,或
  • 基本类型的引用

因此,如果您想要运行时多态性,您可以使用基本类型的 pointerreference,以下是一些如何实现运行时多态性的示例:

So if you want runtime polymorphism, you've use either pointer or reference of base type, and here are few examples how you can achieve runtime polymorphism:

Material* m1 = new Texture();
poly->setMaterial(m1);     //achieved

Texture* t1= new Texture();
poly->setMaterial(t1);     //achieved

Texture t2;
poly->setMaterial( &t2);   //achieved : notice '&'

Material & m2 =  t2;
poly->setMaterial( &m2 );  //achieved : notice '&'

Material  m3;
poly->setMaterial( &m3 );  //NOT achieved : notice '&'

只有在最后一行你没有实现运行时多态性.

Only in the last line you don't achieve runtime polymorphism.

这篇关于为什么虚函数需要通过指针而不是值(对象)传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Polymorphic copy-constructor with type conversion(具有类型转换的多态复制构造函数)
Cast to a Child(铸造给孩子)
How to use virtual functions to achieve a polymorphic behavior in C++?(如何使用虚函数在 C++ 中实现多态行为?)
Polymorphism is not working with function return values of same data type (Base and Inherited class)(多态性不适用于相同数据类型(基类和继承类)的函数返回值)
Size of class with virtual function adds extra 4 bytes(具有虚函数的类的大小增加了额外的 4 个字节)
Right design pattern to deal with polymorphic collections of objects(正确的设计模式来处理对象的多态集合)