由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问个copy constructor的问题
相关主题
问一个 copy constructor 的问题 (C++)一道 memset in C++的题
what is the difference?关于C/C++里的Static variable的memory allocation/initializa
one question about initializaiton list抠字眼:assignment and initialize in C++
c++ initialize structpthread_create inside a constructor
C++ 中 myobject * a =new myobject[n] 的问题Re: VC里面的stl支持是不是很弱?
find bugs of c++ codesc++问题,请高人指点迷津,c++ faq lite的一个例子
question about c++ constructorvector在constructor里初始化
c++ questionc++ 从linux移植到windows上面
相关话题的讨论汇总
话题: copy话题: example话题: e4
进入Programming版参与讨论
1 (共1页)
m********r
发帖数: 334
1
class example {
public:
example(string str){
cout <<"ctor with string "< example() {
cout<<"ctor without string" < example( example& rhs){
cout<<"copy ctor "< };
int main (int argc, char* argv[])
{
string str = "123456789";
example e1(str);
example e2 = str;
example e3 = e1;
example e4 = example();
return 0;
}
这个例子来自C++Primer,在VS2010可以编译,gcc下e2 and e4报错:no matching
function for call to example::example(example)
如果在copy constructor里加上const,gcc可以编译。我的理解是初始化e2 and e4需要
产生临时对象,这个对象是右值,C++标准可能要求形参const以接受右值,为什么VS没
报错不明白。
但是除了e3,copy constructor在初始化e2和e4的时候没有调用,
而C++Primer说这种是copy-initialization,那为什么在VS和gcc下都没有调用copy
constructor?被优化了?
隐式调用copy constructor是不是有编译器决定?
t****t
发帖数: 6806
2
copy-initialization不代表需要用copy constructor, 请勿望文生义

【在 m********r 的大作中提到】
: class example {
: public:
: example(string str){
: cout <<"ctor with string "<: example() {
: cout<<"ctor without string" <: example( example& rhs){
: cout<<"copy ctor "<: };
: int main (int argc, char* argv[])

p******g
发帖数: 347
3
M$'s VS is just not very standard compliant. so try to avoid using it as
much as possible if u r still learning c++.

【在 m********r 的大作中提到】
: class example {
: public:
: example(string str){
: cout <<"ctor with string "<: example() {
: cout<<"ctor without string" <: example( example& rhs){
: cout<<"copy ctor "<: };
: int main (int argc, char* argv[])

f******y
发帖数: 2971
4
e2,e4不需要产生临时对象。
我觉得VS在这个上好像更标准一些。

【在 m********r 的大作中提到】
: class example {
: public:
: example(string str){
: cout <<"ctor with string "<: example() {
: cout<<"ctor without string" <: example( example& rhs){
: cout<<"copy ctor "<: };
: int main (int argc, char* argv[])

t****t
发帖数: 6806
5
99% time, VS is "更不标准". for c++, if VS has different error information
with gcc, 99.99% time VS is wrong.

【在 f******y 的大作中提到】
: e2,e4不需要产生临时对象。
: 我觉得VS在这个上好像更标准一些。

k**********g
发帖数: 989
6

why not 99.
9999999999999999999999999999999999999999999999999999999999999999999999999999
999999999999999999% wrong?
you can add as many 9's as you want. there's no character limit in messages
in mitbbs.

【在 t****t 的大作中提到】
: 99% time, VS is "更不标准". for c++, if VS has different error information
: with gcc, 99.99% time VS is wrong.

t****t
发帖数: 6806
7
你想说啥...99或99.99就是个说法, 我显然没真的统计过.

9999999999999999999999999999999999999999999999999999999999999999999999999999
messages

【在 k**********g 的大作中提到】
:
: why not 99.
: 9999999999999999999999999999999999999999999999999999999999999999999999999999
: 999999999999999999% wrong?
: you can add as many 9's as you want. there's no character limit in messages
: in mitbbs.

t*******c
发帖数: 288
8
http://stackoverflow.com/questions/1051379/is-there-a-differenc
Delving into Initializations Direct and Copy initialization
While they look identical and are supposed to do the same, these two forms
are remarkably different in certain cases. The two forms of initialization
are direct and copy initialization:
T t(x);
T t = x;
There is behavior we can attribute to each of them:
Direct initialization behaves like a function call to an overloaded function
ones), and the argument is x. Overload resolution will find the best
matching constructor, and when needed will do any implicit conversion
required.
Copy initialization constructs an implicit conversion sequence: It tries to
convert x to an object of type T. (It then may copy over that object into
the to-initialized object, so a copy constructor is needed too - but this is
not important below)
As you see, copy initialization is in some way a part of direct
initialization with regard to possible implicit conversions: While direct
initialization has all constructors available to call, and in addition can
do any implicit conversion it needs to match up argument types, copy
initialization can just set up one implicit conversion sequence.
m********r
发帖数: 334
9
如果不需要,为什么没有const的时候GCC编译出错?
C++ Primer里明确说需要用copy constructor
但编译器怎么实现的我就不肯定了

【在 t****t 的大作中提到】
: copy-initialization不代表需要用copy constructor, 请勿望文生义
a*****i
发帖数: 268
10
需要定义一个copy constructor,但不会被调用。
MS的不出错是因为MS的编译器作了extension,可以用non-const reference绑定rvalue
。这个extension可能是为了编译一些old code。可以用/Za来disable,也可以用/W4来
看到warning。

【在 m********r 的大作中提到】
: 如果不需要,为什么没有const的时候GCC编译出错?
: C++ Primer里明确说需要用copy constructor
: 但编译器怎么实现的我就不肯定了

相关主题
find bugs of c++ codes一道 memset in C++的题
question about c++ constructor关于C/C++里的Static variable的memory allocation/initializa
c++ question抠字眼:assignment and initialize in C++
进入Programming版参与讨论
f******y
发帖数: 2971
11
copy initialization在什么情况下有用呢?

function

【在 t*******c 的大作中提到】
: http://stackoverflow.com/questions/1051379/is-there-a-differenc
: Delving into Initializations Direct and Copy initialization
: While they look identical and are supposed to do the same, these two forms
: are remarkably different in certain cases. The two forms of initialization
: are direct and copy initialization:
: T t(x);
: T t = x;
: There is behavior we can attribute to each of them:
: Direct initialization behaves like a function call to an overloaded function
: ones), and the argument is x. Overload resolution will find the best

t****t
发帖数: 6806
12
需要copy constructor不等于用copy constructor, 注意下面最后一句话.
Otherwise (i.e., for the remaining copy-initialization cases), user-defined
conversion sequences that can convert from the source type to the
destination type or (when a conversion function is used) to a derived class
thereof are enumerated as described in 13.3.1.4, and the best one is chosen
through overload resolution (13.3). If the conversion cannot be done or is
ambiguous, the initialization is ill-formed. The function selected is called
with the initializer expression as its argument; if the function is a
constructor, the call initializes a temporary of the cv-unqualified version
of the destination type. The temporary is a prvalue. The result of the call
(which is the temporary for the constructor case) is then used to direct-
initialize, according to the rules above, the object that is the destination
of the copy-initialization. In certain cases, an implementation is
permitted to eliminate the copying inherent in this direct-initialization by
constructing the intermediate result directly into the object being
initialized; see 12.2, 12.8.

【在 m********r 的大作中提到】
: 如果不需要,为什么没有const的时候GCC编译出错?
: C++ Primer里明确说需要用copy constructor
: 但编译器怎么实现的我就不肯定了

d****i
发帖数: 4809
13
你这个e4这样直接调contructor对吗?还是我搞糊涂了?
example e4 = example();
一般按常规应改写成
example e4;

example *e4 = new example();

example *e4 = new example;

【在 m********r 的大作中提到】
: class example {
: public:
: example(string str){
: cout <<"ctor with string "<: example() {
: cout<<"ctor without string" <: example( example& rhs){
: cout<<"copy ctor "<: };
: int main (int argc, char* argv[])

t****t
发帖数: 6806
14
你搞涂了

【在 d****i 的大作中提到】
: 你这个e4这样直接调contructor对吗?还是我搞糊涂了?
: example e4 = example();
: 一般按常规应改写成
: example e4;
: 或
: example *e4 = new example();
: 或
: example *e4 = new example;

d****i
发帖数: 4809
15
这个是C++11的新标准吗?我见过的都不是这么写的,这么写看着有点怪。

【在 t****t 的大作中提到】
: 你搞涂了
f******y
发帖数: 2971
16
不是新标准。

【在 d****i 的大作中提到】
: 这个是C++11的新标准吗?我见过的都不是这么写的,这么写看着有点怪。
d****i
发帖数: 4809
17
是我搞糊涂了,不过非常不喜欢这种创建对象的写法,如果是stack variable,写成
MyClass a;
MyClass a(10, 1);
不就成了吗?写成MyClass a = MyClass()有点脱裤子放屁,多此一举。而且C++的code
style应该尽量保持和C一样,第二种写法非常不好,C里面stack变量不就是
typedef struct {
...
} MyStruct;
MyStruct a;

【在 t****t 的大作中提到】
: 你搞涂了
1 (共1页)
进入Programming版参与讨论
相关主题
c++ 从linux移植到windows上面C++ 中 myobject * a =new myobject[n] 的问题
Is the order of initialization a, b, c or c, b, a?find bugs of c++ codes
question about const referencequestion about c++ constructor
C++ questionc++ question
问一个 copy constructor 的问题 (C++)一道 memset in C++的题
what is the difference?关于C/C++里的Static variable的memory allocation/initializa
one question about initializaiton list抠字眼:assignment and initialize in C++
c++ initialize structpthread_create inside a constructor
相关话题的讨论汇总
话题: copy话题: example话题: e4