How do we use a abstract class as a function return type?

This question already has an answer here:

  • What is a smart pointer and when should I use one? 14 answers

  • Are you asking for example code? Hopefully this helps...

    class Mammal {
    public:
        virtual void Speak() const = 0; 
    };
    
    class Dog : public Mammal {
    public:
        void Speak() const {
            cout << "Woof Woof" << endl;
        }
    };
    
    Mammal* getDog() {
        return new Dog();
    }
    
    int main() {
        Mammal* m = getDog();
        m->Speak();
        delete m;
        return 0;
    }
    
    链接地址: http://www.djcxy.com/p/20772.html

    上一篇: 传递对std :: shared的引用

    下一篇: 我们如何使用抽象类作为函数返回类型?