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:
- 基本类型的指针,或
- 基本类型的引用
因此,如果您想要运行时多态性,您可以使用基本类型的 pointer 或 reference,以下是一些如何实现运行时多态性的示例:
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.
这篇关于为什么虚函数需要通过指针而不是值(对象)传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么虚函数需要通过指针而不是值(对象)传递?
基础教程推荐
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01