Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:多态性:调用超级函数_C++_Polymorphism - Fatal编程技术网

C++ C++;:多态性:调用超级函数

C++ C++;:多态性:调用超级函数,c++,polymorphism,C++,Polymorphism,我有一个动物数组 默认情况下,动物表示“动物” 我还有Cat,它扩展了Animal,改为说“喵” 我还有Dog,它扩展了Animal,改为说“Woof” 我想要的是: Animal* a[2]; a[0] = new Cat(); a[1] = new Dog(); a[0]->talk(); //Meow a[1]->talk(); //Woof 发生了什么: Animal* a[2]; a[0] = new Cat(); a[1] = new Dog(); a[0]->t

我有一个
动物
数组

默认情况下,
动物
表示
“动物”

我还有
Cat
,它扩展了
Animal
,改为说
“喵”

我还有
Dog
,它扩展了
Animal
,改为说
“Woof”

我想要的是:

Animal* a[2];
a[0] = new Cat();
a[1] = new Dog();
a[0]->talk(); //Meow
a[1]->talk(); //Woof
发生了什么:

Animal* a[2];
a[0] = new Cat();
a[1] = new Dog();
a[0]->talk(); //Animal
a[1]->talk(); //Animal

//标题

class Animal {
public:
    Animal();
    ~Animal();
    int talk() {return 0;}
};

class Cat : public  Animal {
public:
    Cat();
    ~Cat();
    int talk() {return 1;}
};

class Dog : public Animal {
public:
    Dog();
    ~Dog();
    int talk() {return 2;}
};
//.cpp

void letsHear(Animal *a) {
    _LOG_INFO() << a->talk();
}

int main(){
    Animal* a[2];
    a[0] = (Animal *)new Cat();
    a[1] = (Animal *)new Dog();

    letsHear((Animal *)new Cat()); //0
    letsHear((Animal *)new Dog()); //0
    letsHear(new Animal()); //0
    letsHear(a[0]); //0
    letsHear(a[1]); //0
    return 0;
}
Cat::Cat() {}
Cat::~Cat(){}
Dog::Dog() {}
Dog::~Dog() {}
Animal::Animal() {}
Animal::~Animal() {}
void letsHear(动物*a){
_LOG_INFO()talk();
}
int main(){
动物*a[2];
一只[0]=(动物*)新猫();
a[1]=(动物*)新狗();
letsHear((动物*)新猫();//0
letsHear((动物*)新狗();//0
letsHear(新动物());//0
letsHear(a[0]);//0
letsHear(a[1]);//0
返回0;
}
Cat::Cat(){}
猫::~Cat(){}
Dog::Dog(){}
狗::~Dog(){}
动物::动物(){}
动物::~Animal(){}

您必须将基本方法
talk()
声明为
virtual
函数。 看看这个例子

class Animal {
public:
    virtual void talk() {
        cout << "Animal" << endl;
    }
};

class Cat : public Animal {
public:
    void talk() {
        cout << "Meow" << endl;
    }
};

class Dog : public Animal {
public:
    void talk() {
        cout << "Woof" << endl;
    }
};

在C++中获取多态行为时,必须声明函数为“代码>虚拟< /代码>。最好在任何多态基类中也使析构函数虚拟:

class Animal {
public:
    Animal();
    virtual ~Animal();
    virtual int talk() {return 0;}
};
#include <iostream>
#include <array>
#include <memory>
using namespace std;

struct Animal {
    virtual void speak() const {
        cout << "Animal" << endl;
    }
    virtual ~Animal() {} // Better add this, too!
};
struct Cat : public Animal {
    virtual void speak() const override {
        cout << "Meow" << endl;
    }
};
struct Dog : public Animal {
    virtual void speak() const override {
        cout << "Wuff" << endl;
    }
};

int main() {
    array<unique_ptr<Animal>, 3> animals;
    animals[0] = make_unique<Cat>();
    animals[1] = make_unique<Dog>();
    animals[2] = make_unique<Animal>();
    for (auto const & a : animals) {
        a->speak();
    }
    return 0;
}
您需要调用的是,即根据要调用其成员函数的对象的类型,选择要在运行时动态调用的函数

在C++中,这是通过使用。要使用它们,请在基类中将相应的成员函数声明为
virtual

class Animal {
public:
    Animal();
    virtual ~Animal();
    virtual int talk() {return 0;}
};
#include <iostream>
#include <array>
#include <memory>
using namespace std;

struct Animal {
    virtual void speak() const {
        cout << "Animal" << endl;
    }
    virtual ~Animal() {} // Better add this, too!
};
struct Cat : public Animal {
    virtual void speak() const override {
        cout << "Meow" << endl;
    }
};
struct Dog : public Animal {
    virtual void speak() const override {
        cout << "Wuff" << endl;
    }
};

int main() {
    array<unique_ptr<Animal>, 3> animals;
    animals[0] = make_unique<Cat>();
    animals[1] = make_unique<Dog>();
    animals[2] = make_unique<Animal>();
    for (auto const & a : animals) {
        a->speak();
    }
    return 0;
}
#包括
#包括

如果(任何)派生类添加了成员字段,并且这些类的任何实例的所有权都掌握在指向基类型的某个指针手中(如我的示例中所示),那么您应该遵循aschelper的建议,并使基析构函数
为虚拟的
。否则,您将导致内存泄漏


您可以看到,我在代码中使用了标准库中的一些概念,如
unique\u ptr
array
以及
for
循环中的类型推断。在适当的时候使用这些数组,在大多数情况下,使用原始数组/指针是不必要的,并且很容易导致内存管理错误。

这不是合法C++。所以指针现在使用<代码> .<代码>?你试过
a[1]->talk()吗发布所有课程。它应该按预期工作,除非你做错了什么。