r*******y 发帖数: 1081 | 1 //1.cpp
class A{ };
int main(){
const A a;
}
this will give compile error:uninitialized const ‘a’
//2.cpp
class A{
public:
A(){}
}
int main(){
const A a;
}
It is ok now. What is the matter to define a default constructor here?
The compiler will give us one default constructor if we don't define
any constructor.
Thanks. |
a***y 发帖数: 2803 | 2 2.cpp里面的
A(){}
这个函数里面是空的吗?
【在 r*******y 的大作中提到】 : //1.cpp : class A{ }; : int main(){ : const A a; : } : this will give compile error:uninitialized const ‘a’ : //2.cpp : class A{ : public: : A(){}
|
r*******y 发帖数: 1081 | 3 yes
【在 a***y 的大作中提到】 : 2.cpp里面的 : A(){} : 这个函数里面是空的吗?
|
M*********t 发帖数: 257 | 4 I think the default constructor added by compiler is private.
thus it can't be used by main()
【在 r*******y 的大作中提到】 : //1.cpp : class A{ }; : int main(){ : const A a; : } : this will give compile error:uninitialized const ‘a’ : //2.cpp : class A{ : public: : A(){}
|
r*******y 发帖数: 1081 | 5 but you can try this
//3.cpp
class A{};
int main(){
A a;
}
At this time, it is OK. The added constructor is public
【在 M*********t 的大作中提到】 : I think the default constructor added by compiler is private. : thus it can't be used by main()
|
a***y 发帖数: 2803 | 6 在visual studio上试了一下,都可以编译成功,但是都有这个warning:
warning C4530: C++ exception handler used, but unwind semantics are not
enabled. Specify /EHsc
【在 r*******y 的大作中提到】 : //1.cpp : class A{ }; : int main(){ : const A a; : } : this will give compile error:uninitialized const ‘a’ : //2.cpp : class A{ : public: : A(){}
|
r*******y 发帖数: 1081 | 7 my god. what is wrong with g++? I use g++
【在 a***y 的大作中提到】 : 在visual studio上试了一下,都可以编译成功,但是都有这个warning: : warning C4530: C++ exception handler used, but unwind semantics are not : enabled. Specify /EHsc
|
t****t 发帖数: 6806 | 8 no no no, usually if g++ and VC has different result, VC is wrong.
according to standard 8.5 clause 9,
9 If no initializer is specified for an object, and the object is of (
possibly cv-qualified) non-POD class type (or array thereof), the object
shall be default-initialized; if the object is of const-qualified type, the
underlying class type shall have a user-declared default constructor.
Otherwise, if no initializer is specified for a nonstatic object, the object
and its subobjects, if any, have an indeterminate initial value; if the
object or any of its subobjects are of const-qualified type, the program is
ill-formed.
【在 r*******y 的大作中提到】 : my god. what is wrong with g++? I use g++
|
r*******y 发帖数: 1081 | 9 this means default ctor added by compiler will not guarantee anything since
it is indeterminated ?
the
object
is
【在 t****t 的大作中提到】 : no no no, usually if g++ and VC has different result, VC is wrong. : according to standard 8.5 clause 9, : 9 If no initializer is specified for an object, and the object is of ( : possibly cv-qualified) non-POD class type (or array thereof), the object : shall be default-initialized; if the object is of const-qualified type, the : underlying class type shall have a user-declared default constructor. : Otherwise, if no initializer is specified for a nonstatic object, the object : and its subobjects, if any, have an indeterminate initial value; if the : object or any of its subobjects are of const-qualified type, the program is : ill-formed.
|
t****t 发帖数: 6806 | 10 you just need to provide a ctor for const object, that's the rule.
since
【在 r*******y 的大作中提到】 : this means default ctor added by compiler will not guarantee anything since : it is indeterminated ? : : the : object : is
|
s******o 发帖数: 2233 | 11 man did u remember all clauses of the standard...
the
object
is
【在 t****t 的大作中提到】 : no no no, usually if g++ and VC has different result, VC is wrong. : according to standard 8.5 clause 9, : 9 If no initializer is specified for an object, and the object is of ( : possibly cv-qualified) non-POD class type (or array thereof), the object : shall be default-initialized; if the object is of const-qualified type, the : underlying class type shall have a user-declared default constructor. : Otherwise, if no initializer is specified for a nonstatic object, the object : and its subobjects, if any, have an indeterminate initial value; if the : object or any of its subobjects are of const-qualified type, the program is : ill-formed.
|
t****t 发帖数: 6806 | 12 of course i don't. but i know where to look at.
【在 s******o 的大作中提到】 : man did u remember all clauses of the standard... : : the : object : is
|