问题遇到的现象和发生背景
多态原理:父类中创建一个虚函数 szieof父类为何运行结果和VS命令提示符中的结果不一样
问题相关代码,请勿粘贴截图
#include<iostream>
#include<string>
using namespace std;
class Animal
{
public:
virtual void speak()
{
cout << "动物在说话" << endl;
}
};
class Cat :public Animal
{
public:
void speak()
{
cout << "小猫在说话" << endl;
}
};
class Dog :public Animal
{
public:
void speak()
{
cout << "小狗在说话" << endl;
}
};
void DoSpeak(Animal& animal)
{
animal.speak();
}
void test01()
{
Cat cat;
DoSpeak(cat);
Dog dog;
DoSpeak(dog);
}
void test02()
{
cout << "sizeof Animal = " << sizeof(Animal) << endl;
}
int main() {
test02();
system("pause");
return 0;
}
运行结果及报错内容
我想要达到的结果
运行后的结果不是也应该为4吗 因为只有一个指针存放在里面