默认构造函数 [英] Default constructor
问题描述
A级
{
int myIntVal;
};
int main( )
{
A a;
返回0;
}
* a *对象中* myIntVal *的值是不确定的。
编译器合成的默认构造函数,不会为* int调用
构造函数*。
为什么?
class A
{
int myIntVal;
};
int main()
{
A a;
return 0;
}
The value of *myIntVal* in *a* object is indetereminate.
The default constructor synthesized by the compiler, does not invoke the
constructor for the *int*.
Why?
推荐答案
Vinodh Kumar写道:
Vinodh Kumar wrote:
A类> {
int myIntVal;
};
int main()
{
A a;
返回0;
* * /
* * * *对象中* myIntVal *的值是不确定的。
编译器合成的默认构造函数不会调用* int *的
构造函数。
为什么?
class A
{
int myIntVal;
};
int main()
{
A a;
return 0;
}
The value of *myIntVal* in *a* object is indetereminate.
The default constructor synthesized by the compiler, does not invoke the
constructor for the *int*.
Why?
类型''int''不是类类型,也没有构造函数。没有什么可以调用的。
如果构造函数的成员中没有提到POD类型的子对象
初始化列表,它根本没有初始化。请参阅
语言标准中的12.6.2 / 4。
-
祝你好运,
Andrey Tarasevich
Brainbench C和C ++编程MVP
Type ''int'' is not a class type and it has no constructor. There''s
nothing to invoke.
If a subobject of POD type is not mentioned in constructor''s member
initializer list, it is not initialized at all. See 12.6.2/4 in the
language standard.
--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP
" Vinodh Kumar" <第************* @ yahoo.com>在消息中写道
news:bg ********** @ news.mch.sbs.de ...
"Vinodh Kumar" <th*************@yahoo.com> wrote in message
news:bg**********@news.mch.sbs.de...
* int *没有非平凡的默认构造函数?
Does *int* has no non-trivial default constructor?
最多只能有一个默认构造函数。这是'
默认调用的那个。在某些情况下初始化一个int,它会弹出存在
只是浪费时间,例如,
int k;
if(some_condition)
{do_something(); k = 2;
}
else
{do_something_else(); k = 3;
}
在这里给k一个初始值可能被认为是一种很好的做法,但它不应该是
不应该'不是强制性的,因为它只是浪费时间和空间。我认为这是因为这个原因,也许是因为与C的兼容性,那么
不是int的默认构造函数。对于一个int类成员来说也是如此。
如果你想让它具有特定的
值,你可以明确地初始化它。
但是,int确实有一个不带参数的非默认构造函数:
int k = int(); // = 0
DW
There can be at most one default constructor. It''s the one that''s called by
default. On some occasions to initialize an int where it pops into existence
is just a waste of time, e.g.,
int k;
if(some_condition)
{ do_something(); k = 2;
}
else
{ do_something_else(); k = 3;
}
To give k an initial value here might be considered good practice, but it
shouldn''t be mandatory because it just wastes time and space. I suppose it
is for this reason, and perhaps for compatibility with C as well, that there
is no default constructor for int. The same goes for an int class member.
It''s up to you to initialize it explicitly if you want it to have a specific
value.
However, int does have a non-default constructor that takes no arguments:
int k = int(); // = 0
DW
Vinodh Kumar写道:
Vinodh Kumar wrote:
> * a *对象中* myIntVal *的值是不确定的。
>由编译器合成的默认构造函数不是
>调用* int *的构造函数。
>为什么?
> The value of *myIntVal* in *a* object is indetereminate.
> The default constructor synthesized by the compiler, does not
> invoke the constructor for the *int*.
> Why?
类型''int''不是类类型,也没有构造函数。没有什么可以调用的。
如果在构造函数的成员
初始化列表中没有提到POD类型的子对象,则它根本没有被初始化。请参阅
语言标准中的12.6.2 / 4.
Type ''int'' is not a class type and it has no constructor. There''s
nothing to invoke.
If a subobject of POD type is not mentioned in constructor''s member
initializer list, it is not initialized at all. See 12.6.2/4 in the
language standard.
确定。
* int *没有非平凡的默认构造函数吗?
OK.
Does *int* has no non-trivial default constructor?
类型''int''不是类类型,它没有构造函数(从上面复制/粘贴
)。没有琐碎或非常规的构造函数,没有默认值
构造函数,根本不是任何构造函数。仅构造函数的概念
存在于C ++中的类类型。
Type ''int'' is not a class type and it has no constructor (copy/paste
from above). No trivial or non-tivial constructor, no default
constructor, not any constructor at all. A concept of constructors only
exists for class types in C++.
这篇关于默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!