如何在C ++结构初始化中获取成员

问题描述 投票:0回答:2
struct Test {
    int w, h;
    int * p;
};

int main(){
    Test t {
        10,
        20,
        new int[this->h*this->w]
    };
    return 0;
}

我只想在初始化中使用w和h,有什么办法做到这一点?

c++ struct this object-construction
2个回答
0
投票
“非静态数据成员按照类定义中的声明顺序进行初始化”。

考虑这个少一些琐碎的例子

struct Test { int w, h; std::unique_ptr<int[]> p; Test(int w_, int h_) : w(w_), h(_), p(std::make_unique<int[]>(w_*h_) { } };
© www.soinside.com 2019 - 2024. All rights reserved.