[英]C++ Modifying properties from a base class
好的,這看起來很簡單,但是當它不起作用時,它會炸毀我的大腦。 這是一個非常簡單的幾個類。 (在VC ++中)
class Food
{
protected:
char maxAmountCarried;
};
class Fruit:Food
{
protected:
Fruit()
{
maxAmountCarried = 8; // Works fine
}
};
class Watermelon:Fruit
{
protected:
Watermelon()
{
maxAmountCarried = 1; //Food::maxAmountCarried" (declared at line 208) is inaccessible
}
};
因此,基本上,我希望默認情況下,水果的最大承載能力為8。西瓜要大得多,因此承載能力更改為1。但是,不幸的是,我無法訪問該屬性。
如果有人可以告訴我解決此問題的方法,那將有很大幫助。
提前致謝 :)
在C ++中,當使用class
作為類鍵來定義類時,默認情況下繼承是私有的。 如果要公共繼承,則必須說:
class Fruit : public Food { /* ... */ };
class Watermelon : public Fruit { /* ... */ };
否則, Food::maxAmountCarried
在Fruit
變為私有 ,並且無法從Watermelon
內部訪問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.