如何初始化聚合成员? [英] How to initialise aggregate member?
问题描述
我们如何初始化类的聚合成员对象?以下
不会为我编译:
struct MyStruct {
int i;
double d;
};
class MyClass {
private:
MyStruct const obj;
public:
MyClass():obj({5,45.67}){}
};
int main()
{
MyClass obj;
}
>
-
Frederick Gotham
How do we initialise an aggregate member object of a class? The following
won''t compile for me:
struct MyStruct {
int i;
double d;
};
class MyClass {
private:
MyStruct const obj;
public:
MyClass() : obj( {5,45.67} ) {}
};
int main()
{
MyClass obj;
}
--
Frederick Gotham
推荐答案
Frederick Gotham写道:
Frederick Gotham wrote:
我们如何初始化类的聚合成员对象?以下
不会为我编译:
How do we initialise an aggregate member object of a class? The following
won''t compile for me:
struct MyStruct {
int我;
加倍;
struct MyStruct {
int i;
double d;
给编译器一些帮助:
MyStruct(int ii,double dd):i(ii),d (dd){}
Give the compiler a little help:
MyStruct(int ii, double dd) : i(ii), d(dd) {}
};
class MyClass {
private:
MyStruct const obj;
public:
MyClass():obj({5,45.67}){}
};
class MyClass {
private:
MyStruct const obj;
public:
MyClass() : obj( {5,45.67} ) {}
切换到:
MyClass():obj(MyStuct(5,45.67)){}
switch it to:
MyClass() : obj(MyStuct(5, 45.67)) {}
};
int main()
{
MyClass obj;
}
};
int main()
{
MyClass obj;
}
并且不用担心构造的额外MyStruct和
复制 - 你的编译器应该优化那个 - 或者
否则切换到更好的编译器。
祝你好运,
Tom
And don''t worry about the extra MyStruct that gets constructed and
copied - your compiler ought to optimize that right out of there - or
else switch to a better compiler.
Best regards,
Tom
Thomas Tutone< Th *********** @ yahoo.comwrote:
Thomas Tutone <Th***********@yahoo.comwrote:
弗雷德ick Gotham写道:
Frederick Gotham wrote:
>我们如何初始化类的聚合成员对象?以下
不会为我编译:
>How do we initialise an aggregate member object of a class? The following
won''t compile for me:
> struct MyStruct {
int i;
双d;
>struct MyStruct {
int i;
double d;
给编译器一些帮助:
MyStruct(int ii,double dd):i(ii),d (dd){}
Give the compiler a little help:
MyStruct(int ii, double dd) : i(ii), d(dd) {}
>};
类MyClass {
私人:
MyStruct const obj;
公开:
MyClass():obj({5,45.67}){}
>};
class MyClass {
private:
MyStruct const obj;
public:
MyClass() : obj( {5,45.67} ) {}
切换到:
MyClass():obj(MyStuct(5,45.67)){}
switch it to:
MyClass() : obj(MyStuct(5, 45.67)) {}
即使这不是必要的;你可以拥有:
MyClass():obj(5,45.67){}
Even that is not necessary; you can just have:
MyClass() : obj(5, 45.67) { }
>};
int main()
{
MyClass obj;
}
>};
int main()
{
MyClass obj;
}
并且不要担心构造的额外MyStruct和
复制 - 你的编译器应该优化那个 - 或者
否则切换到更好的编译器
And don''t worry about the extra MyStruct that gets constructed and
copied - your compiler ought to optimize that right out of there - or
else switch to a better compiler.
-
Marcus Kwok
将''invalid''替换为''net''来回复
--
Marcus Kwok
Replace ''invalid'' with ''net'' to reply
struct MyStruct {
struct MyStruct {
int i;
双d;
int i;
double d;
给编译器一些帮助:
MyStruct(int ii,double dd):i(ii),d (dd){}
Give the compiler a little help:
MyStruct(int ii, double dd) : i(ii), d(dd) {}
};
class MyClass {
private:
MyStruct const obj;
public:
MyClass():obj({5,45.67}){}
};
class MyClass {
private:
MyStruct const obj;
public:
MyClass() : obj( {5,45.67} ) {}
切换到:
MyClass():obj(MyStuct(5,45.67)){}
switch it to:
MyClass() : obj(MyStuct(5, 45.67)) {}
};
int main()
{
MyClass obj;
}
};
int main()
{
MyClass obj;
}
并且不用担心构造的额外MyStruct和
复制 - 你的编译器应该优化那个
And don''t worry about the extra MyStruct that gets constructed and
copied - your compiler ought to optimize that right out of there
为什么要复制它?
MyClass():obj(5,45.67) {}
why copy it at all?
MyClass () : obj (5, 45.67) {}
这篇关于如何初始化聚合成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!