【问题标题】:inherited constructor - define portable fixed-size types (C++)继承的构造函数 - 定义可移植的固定大小类型 (C++)
【发布时间】:2015-12-29 13:01:11
【问题描述】:

我有一个带有构造函数的基类和一个继承类,它有自己的构造函数,它做同样的事情,但还添加了一些额外的指令。

class i_base //Base class for any interface objects
{

public:

    ...//declare some things

    i_base(sf::RenderWindow & rw);

    virtual ~i_base();
};

class IButton
    :public i_base
{
private:

    int w, h;

public:

    IButton(sf::RenderWindow & rw, sf::Image & teximg, 
            int x, int y, int width, int icon);

    ~IButton();
};

问题是当我尝试在继承的类构造函数中初始化基类构造函数时:

IButton::IButton(sf::RenderWindow & rw, sf::Image & teximg, 
                 int x, int y, int width, int icon) 
    : i_base(sf::RenderWindow & rw)
{
    ... //do some things
}

我不会编译,因为我的解析器说 sf::RenderWindow 是不允许的类型,而且它似乎只接受固定大小的类型。所以我已经将 i_base 构造函数的声明更改为有一个 int 作为参数,并且错误消失了,但当然它在我的代码中没有任何意义。有没有办法用非固定大小类型初始化基本构造函数?我试过指针,但它似乎没有解决任何问题。

【问题讨论】:

    标签: c++ inheritance constructor fixed-size-types


    【解决方案1】:

    应该只是

    IButton::IButton(sf::RenderWindow & rw, sf::Image & teximg,
    int x, int y, int width, int icon) : i_base(rw)
    {
        ... //do some things
    }
    

    这意味着将rw 发送到i_base 构造函数。

    【讨论】:

    • 谢谢!我被 cplusplus.com 上关于继承构造函数的类似问题误导了。这家伙在初始化中发布了一个带有类型名称的代码。不知道,也许那是因为,他没有把整个事情分成单独的声明和定义。
    猜你喜欢
    • 2021-11-20
    • 2018-11-06
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多