为什么复制ctor在这里叫了两次? [英] Why is the copy ctor called twice here?

查看:72
本文介绍了为什么复制ctor在这里叫了两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输出:


#include< iostream>

#include< vector>

using namespace std;


struct X {

int i;

X(const X& x):i(xi){

cout<< ctor copy: << i<<结束;

}

X(int ii):i(ii){

cout<< ctor by int: << i<<结束;

}

~X(){

cout<< dtor: << i<<结束;

}

};


int main(int argc,char ** argv){


vector< X> v;

v.push_back(X(100));


返回0;

}


这是:


ctor by int:100

ctor copy:100

ctor copy:100

dtor:100

dtor:100

dtor:100


当你做push_back ,我可以理解想要制作

自己的副本的矢量,但为什么这显然要做两次呢?

The output of this:

#include <iostream>
#include <vector>
using namespace std;

struct X {
int i;
X(const X& x) : i(x.i) {
cout << "ctor copy: " << i << endl;
}
X(int ii) : i(ii) {
cout << "ctor by int: " << i << endl;
}
~X() {
cout << "dtor: " << i << endl;
}
};

int main(int argc, char **argv) {

vector<X> v;
v.push_back(X(100));

return 0;
}

Is this:

ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100

When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?

推荐答案

ri *********** @ yahoo.co.uk 写道:

此输出:

#include< iostream>
#include< vector>
使用命名空间std;

struct X {
int i;
X(const X& x):i(xi){
cout<< ctor copy: << i<<结束;
} X(int ii):i(ii){
cout<< ctor by int: << i<< endl;
}
~X(){
cout<< dtor: << i<< endl;
}
};
int main(int argc,char ** argv){

vector< X> v;
v.push_back(X(100));

返回0;
}

这是:
ctor by int:100
ctor copy:100
ctor copy:100
dtor:100
dtor:100
dtor:100
>当你做push_back时,我可以理解想要制作
自己的副本的矢量,但为什么这显然是两次完成?

The output of this:

#include <iostream>
#include <vector>
using namespace std;

struct X {
int i;
X(const X& x) : i(x.i) {
cout << "ctor copy: " << i << endl;
}
X(int ii) : i(ii) {
cout << "ctor by int: " << i << endl;
}
~X() {
cout << "dtor: " << i << endl;
}
};

int main(int argc, char **argv) {

vector<X> v;
v.push_back(X(100));

return 0;
}

Is this:

ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100

When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?




来自语言从角度来看,没有具体原因。在新闻组中询问

专用于您的特定编译器。它的矢量实现可能是一个错误。


-

Karl Heinz Buchegger
kb ****** @ gascad.at


ri *********** @ yahoo.co.uk 写道:
输出:
< snip program>
这是:

ctor by int:100
ctor copy:100
ctor copy:100 <当你执行push_back时,我可以理解想要制作
自己的副本的向量,但为什么这显然要做两次?
The output of this:
<snip program>
Is this:

ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100

When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?




在我的编译器(g ++ 3.3)上,我得到你所期望的,即:


ctor by int:100

ctor copy:100

dtor:100

dtor:100


我不确定为什么你的矢量表现不同。有可能

push_back按值而不是通过引用(

将是错误的)输入它,或者向量决定延迟比

是必要的(这也是错误的),或其他。如果你希望通过查看vector to source来探索这个,你可以

,你的编译器中应该包含
(因为很少有编译器可以编译

模板分开,因此在标题中包含源代码。


不幸的是我怀疑这样的bug不太可能被修复所以你

可能必须升级您的编译器(尽管您可以随时报告)


Chris



On my compiler (g++ 3.3), I get what you would expect, namely:

ctor by int: 100
ctor copy: 100
dtor: 100
dtor: 100

I am unsure why your vector is behaving differently. It is possible
push_back takes it''s input by value rather than by reference (which
would be wrong), or that the vector is deciding to extend earlier than
is necessary (which would also be wrong), or something else. You could
if you wish explore this by looking at the source to vector, which
should be included in your compiler (as very few compilers can compile
templates seperatly and therefore include the source in the headers).

Unfortunatly I suspect it''s unlikely such a bug would be fixed so you
may have to upgrade your compiler (although you can always report it)

Chris


" Chris Jefferson" < ca*@cs.york.ac.uk>在消息中写道

news:cu ********** @ pump1.york.ac.uk ...
"Chris Jefferson" <ca*@cs.york.ac.uk> wrote in message
news:cu**********@pump1.york.ac.uk...
ri *********** @ yahoo.co.uk 写道:
输出:
< snip program>
这是:

ctor by int:100
ctor copy:100
ctor copy :100
dtor:100


当你做push_back时,我可以理解想要制作的矢量
它自己的副本,但为什么这显然做了两次?
The output of this:
<snip program>
Is this:

ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100

When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?



在我的编译器(g ++ 3.3)上,我得到你所期望的,即:

ctor by int:100
ctor copy:100
dtor:100
dtor:100



On my compiler (g++ 3.3), I get what you would expect, namely:

ctor by int: 100
ctor copy: 100
dtor: 100
dtor: 100




奇怪!在我的编译器(g ++ 3.4.2)上我得到了

ctor by int:100

ctor copy:100

dtor:100

有些事情正在发生!

-

Gary



Odd! On my compiler (g++ 3.4.2) I get
ctor by int: 100
ctor copy: 100
dtor: 100

Something is afoot!
--
Gary


这篇关于为什么复制ctor在这里叫了两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆