由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - const object
相关主题
c++ questionHow to initialize object in constructor?
为啥 const Base cb 要求Base() {} 而 const vBase vb 不呢?被reference搞晕了
一道c++的考古题question regarding const function
What does the default constructor do?const reference in copy constructor
C++ questionC++ 中 myobject * a =new myobject[n] 的问题
question about const referenceC++弱问
c++问题,请高人指点迷津,c++ faq lite的一个例子C++ (direct vs indirect initialization)
what is the difference?回答C++的弱问题
相关话题的讨论汇总
话题: const话题: object话题: default话题: class
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
回答C++的弱问题C++ question
[合集] C++的弱问题question about const reference
急问:compile and build dependencyc++问题,请高人指点迷津,c++ faq lite的一个例子
find bugs of c++ codeswhat is the difference?
c++ questionHow to initialize object in constructor?
为啥 const Base cb 要求Base() {} 而 const vBase vb 不呢?被reference搞晕了
一道c++的考古题question regarding const function
What does the default constructor do?const reference in copy constructor
相关话题的讨论汇总
话题: const话题: object话题: default话题: class