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); //achievedTexture* t1= new Texture();poly->setMaterial(t1); //achievedTexture 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. 这篇关于为什么需要使用指针而不是(对象的)值来传递虚函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-22 18:46