复制构造函数 [英] Copy Constructor

查看:100
本文介绍了复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stoustrup,在C ++编程语言中,有以下示例

in 11.7:


string g(string arg)//按值传递的字符串使用副本

构造函数

{

return arg; //返回字符串(使用复制构造函数)

}


int main()

{

string s =" Newton" ;; //字符串初始化(使用复制构造函数)

s = g(s);

}


前两条评论很清楚。我想知道为什么第三个

评论说初始化使用了复制构造函数。


如果是这样的话,以下代码也不会已经使用复制构造函数初始化了



#include< iostream>


class X {

int i;

public:

X(){i = 0; std :: cout<< " defcon\\\
";}

X(int n){i = n; std :: cout<< " intcon\\\
";}

X(const X& x){i = x.i; std :: cout<< copycon \ n;}}

};


int main()

{

X x = 2;

}


相反,输出为:intcon

LRSK

解决方案

2004年1月10日星期六05:17:51 -0800,LRS Kumar写道:

Stoustrup,在C ++中编程语言,在11.7中有以下示例


字符串g(字符串arg)//使用copy传递值的字符串
构造函数
{
返回arg; //返回字符串(使用复制构造函数)



{
string s =" Newton" ;; //字符串初始化(使用复制构造函数)
s = g(s);
}

前两条评论很清楚。我想知道为什么第三个评论说初始化使用了复制构造函数。

如果是这样的话,以下代码也不会被初始化使用复制构造函数?

#include< iostream>

X类{
int i;
public:
X(){ I = 0; std :: cout<< " defcon\\\
";}
X(int n){i = n; std :: cout<< " intcon\\\
";}
X(const X& x){i = x.i; std :: cout<< " copycon\\\
";}
};

int main()
{
X x = 2;
}

相反,输出是:intcon




复制构造函数*在概念上*在这里使用。所以它必须存在并且可以访问
。 IIRC允许编译器优化它。


HTH,

M4


" LRS Kumar" < LR ****** @ yahoo.co.in>在消息中写道

news:d0 ************************** @ posting.google.c om ...

Stoustrup,在C ++编程语言中,在11.7中有以下示例


字符串g(字符串arg)//使用copy传递值的字符串<建设者
{
返回arg; //返回字符串(使用复制构造函数)



{
string s =" Newton" ;; //字符串初始化(使用复制构造函数)
s = g(s);
}

前两条评论很清楚。我想知道为什么第三个评论说初始化使用了复制构造函数。

如果是这样的话,以下代码也不会被初始化使用复制构造函数?

#include< iostream>

X类{
int i;
public:
X(){ I = 0; std :: cout<< " defcon\\\
";}
X(int n){i = n; std :: cout<< " intcon\\\
";}
X(const X& x){i = x.i; std :: cout<< " copycon\\\
";}
};

int main()
{
X x = 2;
}

相反,输出是:intcon

LRSK




你错误地引用了这本书。 BS无处可说


string s =" Newton"


是复制构造函数的一个例子。再读一遍。


-

Cy
http://home.rochester.rr.com/cyhome/


" Cy Edmunds" < CE ****** @ spamless.rochester.rr.com>写道......

" LRS Kumar" < LR ****** @ yahoo.co.in>在消息中写道
新闻:d0 ************************** @ posting.google.c om ...

Stoustrup,在C ++编程语言中,在11.7中有以下示例


string g(string arg)//使用copy
构造函数通过值传递的字符串
{
返回arg; //返回字符串(使用复制构造函数)



{
string s =" Newton" ;; //字符串初始化(使用复制构造函数)
s = g(s);
}

前两条评论很清楚。我想知道为什么第三个评论说初始化使用了复制构造函数。

如果是这样的话,以下代码也不会被初始化使用复制构造函数?

#include< iostream>

X类{
int i;
public:
X(){ I = 0; std :: cout<< " defcon\\\
";}
X(int n){i = n; std :: cout<< " intcon\\\
";}
X(const X& x){i = x.i; std :: cout<< " copycon\\\
";}
};

int main()
{
X x = 2;
}

相反,输出是:intcon

LRSK



你错误地引用了这本书。 BS无处可说

string s =" Newton"

是复制构造函数的一个例子。再读一遍。




这不是一个例子,但复制构造函数在这样的初始化中是_used_

。有关

语义的解释,请参见8.5 / 15。这个表格的名称是复制初始化

,可以在8.5 / 12中找到。


即使复制结构已经优化(如允许),

a复制构造函数必须是可访问的,好像没有优化是

执行。


Victor


Stoustrup, in The C++ Programming Language, has the following example
in 11.7:

string g (string arg) // string passed by value using copy
constructor
{
return arg; //string returned (using copy constructor)
}

int main()
{
string s = "Newton"; //string initialized (using copy constructor)
s = g(s);
}

The first two comments are clear. I was wondering why the third
comment says that the initialization uses the copy constructor.

If that was the case, wouldn''t the following code also have been
initialized using the copy constructor?
#include <iostream>

class X {
int i;
public:
X() { i=0; std::cout << "defcon\n";}
X(int n) {i=n; std::cout << "intcon\n";}
X(const X& x) {i = x.i; std::cout << "copycon\n";}
};

int main()
{
X x = 2;
}

Instead, the output is: intcon
LRSK

解决方案

On Sat, 10 Jan 2004 05:17:51 -0800, LRS Kumar wrote:

Stoustrup, in The C++ Programming Language, has the following example
in 11.7:

string g (string arg) // string passed by value using copy
constructor
{
return arg; //string returned (using copy constructor)
}

int main()
{
string s = "Newton"; //string initialized (using copy constructor)
s = g(s);
}

The first two comments are clear. I was wondering why the third
comment says that the initialization uses the copy constructor.

If that was the case, wouldn''t the following code also have been
initialized using the copy constructor?
#include <iostream>

class X {
int i;
public:
X() { i=0; std::cout << "defcon\n";}
X(int n) {i=n; std::cout << "intcon\n";}
X(const X& x) {i = x.i; std::cout << "copycon\n";}
};

int main()
{
X x = 2;
}

Instead, the output is: intcon



The copy constructor is *conceptually* used here. So it must exist and be
accessible. IIRC the compiler is allowed to optimize it away though.

HTH,
M4


"LRS Kumar" <lr******@yahoo.co.in> wrote in message
news:d0**************************@posting.google.c om...

Stoustrup, in The C++ Programming Language, has the following example
in 11.7:

string g (string arg) // string passed by value using copy
constructor
{
return arg; //string returned (using copy constructor)
}

int main()
{
string s = "Newton"; //string initialized (using copy constructor)
s = g(s);
}

The first two comments are clear. I was wondering why the third
comment says that the initialization uses the copy constructor.

If that was the case, wouldn''t the following code also have been
initialized using the copy constructor?
#include <iostream>

class X {
int i;
public:
X() { i=0; std::cout << "defcon\n";}
X(int n) {i=n; std::cout << "intcon\n";}
X(const X& x) {i = x.i; std::cout << "copycon\n";}
};

int main()
{
X x = 2;
}

Instead, the output is: intcon
LRSK



You have misquoted the book. Nowhere does BS say that

string s = "Newton";

is an example of a copy constructor. Read it again.

--
Cy
http://home.rochester.rr.com/cyhome/


"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote...

"LRS Kumar" <lr******@yahoo.co.in> wrote in message
news:d0**************************@posting.google.c om...

Stoustrup, in The C++ Programming Language, has the following example
in 11.7:

string g (string arg) // string passed by value using copy
constructor
{
return arg; //string returned (using copy constructor)
}

int main()
{
string s = "Newton"; //string initialized (using copy constructor)
s = g(s);
}

The first two comments are clear. I was wondering why the third
comment says that the initialization uses the copy constructor.

If that was the case, wouldn''t the following code also have been
initialized using the copy constructor?
#include <iostream>

class X {
int i;
public:
X() { i=0; std::cout << "defcon\n";}
X(int n) {i=n; std::cout << "intcon\n";}
X(const X& x) {i = x.i; std::cout << "copycon\n";}
};

int main()
{
X x = 2;
}

Instead, the output is: intcon
LRSK



You have misquoted the book. Nowhere does BS say that

string s = "Newton";

is an example of a copy constructor. Read it again.



It''s not an example of one, but a copy constructor is _used_
in such initialisation. See 8.5/15 for the explanation of
the semantics. The name for this form is "copy initialisation"
and can be found in 8.5/12.

Even if the copy-construction is optimised (as permitted),
a copy-constructor must be accessible as if no optimisation is
performed.

Victor


这篇关于复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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