[英]C++ Inheritance problem
I hope I got the relevant code in here. 我希望我在这里得到相关的代码。 I have some problem when I want to fetch the menu option that I've added into to
menu_1
. 当我想获取添加到
menu_1
的菜单选项时遇到一些问题。 I have this function on_select(int)
that I use to fetch one sub-menu's options, which I do by using the display()
function. 我使用此函数
on_select(int)
来获取一个子菜单的选项,这是通过使用display()
函数来完成的。 But when I compile it will say that there are no function named display() in menu_option() class, which is the Base class, but what I want to is to access the display() function which is located in the sub_menu() class. 但是当我编译它会说在menu_option()类中没有名为display()的函数,它是Base类,但我想要的是访问位于sub_menu()类中的display()函数。
I have tried multiple thing to get the relevant object from the array without any success, so I'm here now asking for help with this one. 我已经尝试了多种方法来从数组中获取相关对象,但均未成功,因此我现在在此寻求帮助。
I have this following main() 我有以下main()
#include <iostream>
using namespace std;
#include "menu.h"
int main()
{
sub_menu* main_menu = new sub_menu("Warehouse Store Menu");
sub_menu* menu_1 = new sub_menu("Menu1");
main_menu->add_option(new sub_menu("Menu2"));
main_menu->add_option(menu_1);
product_menu->add_option(new add_item("sub_item1"));
product_menu->add_option(new add_item("sub_item2"));
product_menu->add_option(new add_item("sub_item3"));
main_menu->display();
main_menu->on_select(1);
delete main_menu;
return 0;
}
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
const int MAX_SIZE = 9;
class menu_option
{
public:
menu_option(string const& n) : title(n) {};
virtual ~menu_option();
virtual void on_select(int) = 0;
string get_title() { return title; }
protected:
string title;
};
/* ------------------------------------- */
class sub_menu : public menu_option
{
public:
sub_menu(string const& n)
: menu_option(n) { no_item = 0; }
~sub_menu() { delete[] list; };
void on_select(int);
void add_option(menu_option*);
void display();
private:
menu_option* list[MAX_SIZE]; //container for options in the sub_menu
int no_item;
};
void sub_menu::on_select(int i)
{
cout << (list[i])->get_title() << endl;
cout << (list[i])->display() << endl; //<------ Doesn't work
}
void sub_menu::add_option(menu_option* item)
{
list[no_item] = item;
no_item++;
}
void sub_menu::display()
{
cout << ">> " << get_title() << " <<"<< endl;
for( int i = 0; i < no_item; i++ )
{
cout << setw(2) << i << ": " << (list[i])->get_title() << endl;
}
}
To solve your immediate problem you'll want to use dynamic_cast
to turn a menu_option*
into a sub_menu*
, like so: 要解决您的直接问题,您需要使用
dynamic_cast
将menu_option*
转换为sub_menu*
,如下所示:
sub_menu* submenu(dynamic_cast<sub_menu*>(list[i]));
Note that if the cast fails (ie, the menu_option
pointed to by list[i]
is not a sub_menu
after all) the value of the submenu pointer will be NULL, so make sure you check that it is a valid pointer before using it in subsequent code. 请注意,如果
menu_option
失败(即list[i]
指向的sub_menu
毕竟不是sub_menu
),则子菜单指针的值将为NULL,因此请确保在使用它之前先检查它是否为有效指针。后续代码。
You can do what you want to do, but it's bad. 你可以做你想做的事,但这很糟糕。 You have to cast down to sub_menu when you call display() in on_select().
在on_select()中调用display()时,您必须向下推至sub_menu。 Of course it's not going to work the way you have it, and the compiler is telling you exactly why.
当然,它无法按您的方式工作,编译器会告诉您原因。
The other option, which is probably better (though without a clear understanding of the problem space may not be the best ) would be to add display() as a virtual function to the menu_option class. 另一个选项,可能更好(虽然没有清楚地理解问题空间可能不是最好的 )是将display()作为虚拟函数添加到menu_option类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.