向量和参考 [英] Vectors and References

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

问题描述

我想知道STL矢量是如何复制我的

对象的。例如:


#include< iostream>

#include< cstdlib>

#include< vector>

使用命名空间std;


班级测试

{

私人:

int value;


public:

test():value(0){}

~test(){ }


const int& getValue()const {

返回this-> value;

}


int setValue(int value){

this-> value = value;

}

};


int main(int argc ,char * argv [])

{

vector< test> vals;

test val;

vals.push_back(val);

val.setValue(5);

cout<< val.getValue()<< endl;

cout<< vals [0] .getValue()<< endl;


返回0;

}

// ------结束示例代码


这个输出:

5

0


我期待他们两个输出0。矢量

是如何保留值0的呢?难道它不仅仅是对我的对象提供了
的引用吗?既然我还没有定义一个拷贝构造函数,那么它是如何受到val.setValue(5)影响的呢?

解决方案

< jo *********** @ gmail.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...

我想知道STL矢量是如何制作我的
对象的副本的。


它制作副本,存储副本并拥有它们。

例如:

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

课堂测试
{
私人:
int value;

public:
test():value(0){}
~test(){}

const int& getValue()const {
返回this-> value;
}
int setValue(int value){
this-> value = value;
}
};

int main(int argc,char * argv [])
{
vector< test> vals;
测试val;
vals.push_back(val);


vals有一个值为0的对象。

val.setValue(5);


val是一个值为5的本地对象。

cout<< val.getValue()<< endl;
cout<< vals [0] .getValue()<< endl;

返回0;
}
// ------结束示例代码

此输出:
5
0

我期待他们两个输出0.如何向量
能够保留值0?它不仅仅是对我的对象的引用吗?


否;它保存了你的对象的副本。

因为我还没有定义一个拷贝构造函数,它是如何不受val.setValue(5)影响的呢? / blockquote>


编译器定义了一个复制构造函数,如果你没有声明一个。那个副本

构造函数递归地调用基础和成员的副本构造函数。


一些非常好的书会浮现在脑海中:


- Scott Meyers的有效C ++系列

- Josuttis的C ++标准库


Ali

-

插头:ACCU的硅谷分会在第二个星期二开会。会议

免费向公众开放。今晚请来谈谈

Ada:

http://accu-usa.org/index.html


>如果您没有声明,编译器会定义一个复制构造函数。该副本

构造函数递归调用base和members的复制构造函数。




太棒了!谢谢,这正是我想知道的。另外,对于我来说,仅仅让编译器定义它自己的

复制构造函数对我来说一定是件坏事吗?特别是对于大型对象,让编译器自动执行它会更容易。

。这有什么缺点

,如果有的话?




< jo ** *********@gmail.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...

我想知道STL矢量是如何制作我的
对象的副本的。例如:

#include< iostream>
#include< cstdlib>
#include< vector>
使用命名空间std;
<课堂测试
{
私人:
int value;

公开:
test():value(0){}
~test(){}

const int& getValue()const {
返回this-> value;
}
int setValue(int value){
this-> value = value;
}
};

int main(int argc,char * argv [])
{
vector< test> vals;
测试val;
vals.push_back(val);
val.setValue(5);

cout<< val.getValue()<< endl;
cout<< vals [0] .getValue()<< endl;

返回0;
}
// ------结束示例代码

此输出:
5
0

我期待他们两个输出0.如何向量
能够保留值0?它不仅仅是对我的对象的引用吗?既然我还没有定义一个拷贝构造函数,那么它是如何受到val.setValue(5)影响的呢?




你为什么期望它们都输出0? (你的意思是5?)你已经明确地为val调用了setValue(5)
。所以它必须报告5.但是

向量有一个val的副本,而不是它的引用或指针。当你调用

push_back,传递val时,会产生一个副本,就像你将一个对象

按值传递给任何函数一样。该对象的副本存储在

向量中。该副本的值为0,因为当它被创建时它就是那里的。


事实上你没有定义一个复制构造函数不会阻止复制

对象。它仅仅意味着默认。 copy

构造函数 - 在没有用户的情况下由编译器创建

定义的 - 被调用。该版本只复制

成员的值(而不是复制构造成员,其中

需要)。 (该拷贝构造函数是0值的来源,因为

在复制时,val在其值成员中保持为0。)如果int成员

"值"是你唯一的会员,没关系。但是,如果你添加指针ot

引用成员,或std:string或其他复杂成员之类的东西,

你需要添加自己的拷贝构造函数。 br />

-Howard


Hi, I''m wondering exactly how the STL vector is making a copy of my
object. For example:

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

class test
{
private:
int value;

public:
test():value(0){}
~test(){}

const int& getValue() const {
return this->value;
}

int setValue(int value) {
this->value = value;
}
};

int main(int argc, char *argv[])
{
vector<test> vals;
test val;
vals.push_back(val);
val.setValue(5);

cout << val.getValue() << endl;
cout << vals[0].getValue() << endl;

return 0;
}
//------ End sample code

This outputs:
5
0

I was expecting both of them to output 0. How is it that the vector
was able to preserve the value 0? Doesn''t it just hold a reference to
my object? And since I haven''t defined a copy constructor, how is it
that it''s not affected by val.setValue(5)?

解决方案

<jo***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

Hi, I''m wondering exactly how the STL vector is making a copy of my
object.
It makes copies, stores the copies, and owns them.
For example:

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

class test
{
private:
int value;

public:
test():value(0){}
~test(){}

const int& getValue() const {
return this->value;
}

int setValue(int value) {
this->value = value;
}
};

int main(int argc, char *argv[])
{
vector<test> vals;
test val;
vals.push_back(val);
vals have one object with value 0.
val.setValue(5);
val is a local object with value 5.
cout << val.getValue() << endl;
cout << vals[0].getValue() << endl;

return 0;
}
//------ End sample code

This outputs:
5
0

I was expecting both of them to output 0. How is it that the vector
was able to preserve the value 0? Doesn''t it just hold a reference to
my object?
No; it holds copies of your objects.
And since I haven''t defined a copy constructor, how is it
that it''s not affected by val.setValue(5)?



Compiler defines a copy constructor if you don''t declare one. That copy
constructor calls the copy constructors of bases and members recursively.

Some very good books come to mind:

- Effective C++ series by Scott Meyers
- The C++ Standard Library by Josuttis

Ali
--
Plug: ACCU''s Silicon Valley Chapter meets on second Tuesdays. The meetings
are open to public and free of charge. Please come tonight for a talk on
Ada:

http://accu-usa.org/index.html


> Compiler defines a copy constructor if you don''t declare one. That copy

constructor calls the copy constructors of bases and members recursively.



Great! Thanks, that exactly what I wanted to know. Also, is it
necessarily a bad thing for me to just let the compiler define it''s own
copy constructor? Especially for large objects, it would just be easier
for me to let the compiler automatically do it. What is the drawback
to this, if any?



<jo***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

Hi, I''m wondering exactly how the STL vector is making a copy of my
object. For example:

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

class test
{
private:
int value;

public:
test():value(0){}
~test(){}

const int& getValue() const {
return this->value;
}

int setValue(int value) {
this->value = value;
}
};

int main(int argc, char *argv[])
{
vector<test> vals;
test val;
vals.push_back(val);
val.setValue(5);

cout << val.getValue() << endl;
cout << vals[0].getValue() << endl;

return 0;
}
//------ End sample code

This outputs:
5
0

I was expecting both of them to output 0. How is it that the vector
was able to preserve the value 0? Doesn''t it just hold a reference to
my object? And since I haven''t defined a copy constructor, how is it
that it''s not affected by val.setValue(5)?



Why would you expect them both to output 0? (Did you mean 5?) You''ve
explicitly called setValue(5) for val. So it has to report 5. But the
vector has a copy of val, not a reference or pointer to it. When you call
push_back, passing val, a copy is made, the same as if you passed an object
by value to any function. A copy of that object is what is stored in the
vector. That copy has the value 0 in it, since that was what was there when
it was created.

The fact that you didn''t define a copy constructor does not prevent the
object from being copied. It merely means the that "default" copy
constructor - which gets created by the compiler in the absence of a user
defined one - gets called. That version just copies the values of the
members (as opposed to copy-constructing the members as well, where
required). (That copy constructor is where that 0 value came from, since
when the copy was made, val held 0 in its value member.) If the int member
"value" is your only member, that is fine. But, if you add pointer ot
reference members, or things like std:string or other complex members,
you''ll need to add you own copy constructor.

-Howard


这篇关于向量和参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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