【问题标题】:Using inheritance to pass a non-static variable between classes使用继承在类之间传递非静态变量
【发布时间】:2012-02-21 22:36:08
【问题描述】:

我正在研究 UI 作为一个投资组合项目,但在继承方面遇到了一些麻烦。我面临的问题是:我有两个类,R_GUI,我在其中绘制表单,和 Button,我在其中绘制按钮。我希望将按钮定位在 FORM 内。我将 FORM 位置存储为 form_x 和 form_y。这是我的两个课程:

class R_GUI 
{

private:


    bool get_pos_only_once;
    bool move_form;

public:
    int form_x, form_y;
    int form_height, form_width;
    int handle_bar_y;

    inline void Form(int pos_x, int pos_y, int height, int width);
    //inline void Button(int id, string ButtonText, int pos_x, int pos_y);

    inline void Update_form(void);

    inline void UPDATE(void);

    inline ~R_GUI( );
    inline R_GUI( )
    {
        d3dInit();
        get_pos_only_once = false;
        move_form = false;
        form_x = 0;
        form_y = 0;
        handle_bar_y = 40;
    }
};

还有 Button 类:

class Button: public R_GUI
{

private:
    int b_form_x, b_form_y;
    int b_handle_y;
    int button_width, button_height;

public:
    inline void Draw(string ButtonText, int b_pos_x, int b_pos_y);
    Button()
    {

        b_form_x = R_GUI::form_x;
        b_form_y = R_GUI::form_y;
        b_handle_y = 20;

        button_width = 90;
        button_height = 35;
    }

    ~Button();



};

如您所见,我试图为 b_form_x 赋予 form_x 的值(这是 R_GUI 中的一个变量)。 form_x 有一个值,在 Form() 中给出;

inline void R_GUI::Form(int pos_x, int pos_y, int height, int width)
{
    if(get_pos_only_once == false)
    {
        form_x = pos_x;
        form_y = pos_y;
        form_height = height;
        form_width = width;

        get_pos_only_once = true;
    }

    //Create the form outline
    d3dLine(pos_x,pos_y,pos_x+width,pos_y,dbRGB(50,50,50));
    d3dLine(pos_x,pos_y,pos_x,pos_y+height,dbRGB(50,50,50));
    d3dLine(pos_x+width,pos_y,pos_x+width,pos_y+height,dbRGB(50,50,50));
    d3dLine(pos_x,pos_y+height,pos_x+width,pos_y+height,dbRGB(50,50,50));

    //Create the handle bar
    d3dLine(pos_x,pos_y+handle_bar_y,pos_x+width,pos_y+handle_bar_y,dbRGB(50,50,50));

    //Fill the Handlebar;
    d3dBox(pos_x,pos_y,pos_x+width,pos_y+handle_bar_y,dbRGB(3,3,3),dbRGB(3,3,3),dbRGB(3,3,3),dbRGB(3,3,3));


}

然而,当我更新表单的位置时,R_GUI::form_x 的值并没有改变。知道我做错了什么吗?

【问题讨论】:

    标签: c++ class variables inheritance


    【解决方案1】:

    您不想在子类中包含第二个变量 (b_form_x)。完全消除它。相反,您想使用this->form_x,它在Button 中将继承自R_GUI。这同样适用于b_form_y。

    this 指的是对象的当前实例,不仅包含您的本地成员变量,还包含从层次结构中的类继承的任何成员变量。

    【讨论】:

    • 嗨@Borealid,感谢有关此的提示->。嗯,问题是我在更新Window的时候更新了form_x/form_y,但是Button类里面的值不变。
    • @BacioiuCiprian 那是因为它们是不同的变量,并且您忘记了从一个复制到另一个(具体来说,您只在构造函数中进行复制,而不是在 R_GUI::Form 中进行复制)。你希望它们是相同的,并且没有那个困难。
    • 我在 R_GUI::UpdateForm() 中更新 form_x,并且在绘制按钮时使用 this->form_x(在 Button::Draw() 中)。跨度>
    • @BacioiuCiprian 我没有看到UpdateForm 正文。
    • 这是 Update_form 函数:inline void R_GUI::Update_form(void) { if(dbMouseX() >= form_x && dbMouseX() <= form_x+form_width) { if(dbMouseY() >= form_y && dbMouseY() <= form_y+handle_bar_y) { if(dbMouseClick()) { move_form = true; } else { move_form = false; } } } if(move_form == true) { form_x = dbMouseX()-form_width/2; form_y = dbMouseY(); } }
    猜你喜欢
    • 2020-10-18
    • 2012-07-17
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    相关资源
    最近更新 更多