我有以下程序
#include <iostream>
class Animal {
public:
Animal(int age) : age_(age) {}
virtual ~Animal() {}
virtual void vocalize() = 0;
virtual void description() = 0;
protected:
int age_;
};
class Dog : public Animal {
public:
Dog(int age, bool bites) : Animal(age), bites_(bites) {}
virtual ~Dog() {}
virtual void vocalize() {
std::cout << "woff" << std::endl;
}
virtual void description() {
std::cout << "Dog" << std::endl;
std::cout << "age " << age_ << std::endl;
std::cout << "bites " << bites_ << std::endl;
}
private:
bool bites_;
};
class Cat : public Animal {
public:
Cat(int age, bool likes_tuna) : Animal(age), likes_tuna_(likes_tuna) {}
virtual ~Cat() {}
virtual void vocalize() {
std::cout << "meow" << std::endl;
}
virtual void description() {
std::cout << "Cat" << std::endl;
std::cout << "age " << age_ << std::endl;
std::cout << "likes tuna " << likes_tuna_ << std::endl;
}
private:
bool likes_tuna_;
};
int doStuff(Animal &animal) {
animal.description();
animal.vocalize();
}
int main() {
Dog d(3, false);
Cat c(5, true);
doStuff(d);
doStuff(c);
}
如您所见,当我调用 doStuff 时,我将派生类传递给绑定到基类引用,然后调用纯虚方法,并由每个派生类重新实现。其中一种方法(描述)使用基类(age_)上的成员变量和派生类(likes_tuna_和bites_)上的变量。我会被切片还是合法?该程序似乎有效,但可能是偶然的。