Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 初始化函数调用,初始化列表需要哪些效果?_C++_Initialization List - Fatal编程技术网

C++ 初始化函数调用,初始化列表需要哪些效果?

C++ 初始化函数调用,初始化列表需要哪些效果?,c++,initialization-list,C++,Initialization List,我有一个包含常量字段成员的基本映像类: class Image { protected: const int width; const int height; public: virtual ~Image(); const int getWidth() const; const int getHeight() const; protected: Image(const int width, const int height); }; 这意味着

我有一个包含常量字段成员的基本映像类:

class Image {
protected:
    const int width;
    const int height;

public:
    virtual ~Image();
    const int getWidth() const;
    const int getHeight() const;

protected:
    Image(const int width, const int height);
};
这意味着在构造函数中,我必须初始化初始化列表中的宽度和高度,如下所示:

Image::Image(const int width, const int height) :
        width(width), height(height) {
}
现在我打算将其子类化,以便通过提供子类的文件路径来加载映像(因此调用代码不必担心加载映像)。此类将类似于以下内容:

class GlImage: public Image {
private:
    const GLuint textureId;
    const int textureWidth;     // power of 2 width
    const int textureHeight;    // power of 2 height
    const double textureCoordinateX;
    const double textureCoordinateY;

public:
    GlImage(const string path); // is this constructor possible?
    virtual ~GlImage();
    const double getTextureCoordinateX() const;
    const double getTextureCoordinateY() const;

private:
    // what do we use for initialization functions?
};
但是我们在这里可以看到, -必须先加载图像,然后才能获得宽度/高度 -子类中也有需要在初始化列表中初始化的字段

如何设置子类的构造函数,使所有这些字段都可以初始化

不可能在子类中引入另一个对象来捕获所有字段数据,将其作为初始化列表中的第一个对象加载,并重新提取所有其他字段的值,因为基类需要宽度/高度(如果图像加载逻辑在派生类中,则不可用)


删除常量修饰符并初始化构造函数中的字段的唯一方法是什么?

问题是您不能从派生类构造函数初始值设定项列表中调用基类构造函数,因为您不知道维度,对吗

为什么不在派生类上创建一个静态方法呢

class GlImage: public Image {
    ....
    static GlImage LoadFromFile(const string& path)
    {
         // read image data and other info
         int w = ...
         int h = ...
         return GlImage(w, h, ....) 
         // of course, you would need a different ctor in derived class
         // it can even be private
    }
 }

您可以将委托构造函数与执行加载的可变辅助对象一起使用:

class GlImage: public Image {
    ...
    GlImage(const string& path) : GlImage(GlImageLoader(path))
    {
    }

private:
    struct GlImageLoader {
        int w;
        int h;
        GlImageLoader(const string& path) {
            //Read image data
            w = ...;
            h = ...;
        }
    };
    GlImage(GlImageLoader&& l) : Image(l.w, l.h), textureId(l.textureId), ...
    {
    }
};

从成员初始化列表调用基构造函数。最好在基本构造函数中稍微更改参数名称。事情可能会一团糟。@CodingMash OP知道这一点……但你用什么来称呼它呢?另一种方法(大致相同)是使用一个带有可变成员的临时辅助对象的委托构造函数。Zdeslav:是的,这就是问题所在,我想不可避免地要有一个构造函数来接受所需的参数。谢谢我可能会有一个工厂类来处理加载,这样GlImage就纯粹是一个数据容器(如果我从这个设计开始,我就不会遇到这个问题)@曼卡斯:你能给我举个例子吗,因为我还没弄明白。