的調用方法我有如下被初始化Item
對象的動態列表:從父類對象的用C子類++
Item * items = new Item[size];
其中size
是先前initialzed的整數。
我使用Book
對象填充此列表,其中Book
是Item
的子項。 Book
和Item
類都有方法print
,它向控制檯輸出一定數量的信息。我希望能夠調用items[x].print()
以調用Book
的print
方法,它看起來像這樣:
void print() {
Item::print(); //call to print method of parent class
<printing out some information>
}
基本上,我想調用子類Book
,這反過來又調用print
的print
方法方法Item
,它打印出更一般的信息。但是,使用items[x].print()
僅調用Item
中的print
方法。我如何在Item
類型的數組items
中調用Book
類的print
方法?
Q;所以你有Book :: print()調用Item :: print()?然後你還想讓Item :: print()調用Book :: print()?問:當你嘗試時會發生什麼? – paulsm4
它只調用Item中的打印方法,而不是Book。 – applemavs
'print'虛擬? – John3136