-7
#include <iostream>
using namespace std;
class animal
{
public:
void breathe()
{
cout << "breathe!" << endl;
}
int height;
};
class fish : public animal
{
public:
void breathe()
{
cout << "fish breathe!" << endl;
}
int weight;
};
int main()
{
animal *p_animal = new animal();
fish *p_fish = (fish *)p_animal;
p_fish->breathe();
p_fish->weight = 2;
cout << p_fish->weight; //I new a animal instance,but why does it has weight property?
int temp;
cin >> temp;
}
編譯器不會阻止你自己拍攝。你走出去的方式來做到這一點,代碼現在是越野車。 – chris
你正在使用你不應該的內存。您的代碼受未定義的行爲影響。 –
它沒有「重量」屬性,你只是假裝它。假裝可以讓你快樂一段時間,直到現實提醒你,世界不是那樣的。然後你學會不要對編譯器說謊。 – molbdnilo