Learning C++: returning references AND getting around slicing

前端 未结 8 1388
醉酒成梦
醉酒成梦 2021-02-14 00:06

I\'m having a devil of a time understanding references. Consider the following code:

class Animal
{
public:
    virtual void makeSound() {cout << \"rawr\"         


        
8条回答
  •  鱼传尺愫
    2021-02-14 00:39

    If you want to return a polymorphic type from a method and don't want to assign it on the heap you could consider making it a field in that method's class and making the function to return a pointer to it of whatever base class you want.

提交回复
热议问题