关于赋值运算符的问题 [英] question on the assignment operator

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

问题描述



考虑:


#include< iostream>

#include< algorithm>

#include< vector>

#include< string>

使用命名空间std;


class msg {

std :: string someStr;

public:

msg(const std :: string& sStr)

:someStr(sStr){}

msg(const msg& rhs)

:someStr(rhs.someStr)

{}

void swap(msg& rhs){

std :: swap(someStr,rhs.someStr);

}

味精和放大器; operator =(const msg& rhs){

msg tmp(rhs);

swap(tmp);

return * this;

}

std :: string get_some_str()const {return someStr; }

};


class Y {

typedef std :: vector< msg> msgVec;

msgVec msg_vec;

public:

Y(){}

Y(const Y& rhs )

:msg_vec(rhs.msg_vec)

{}

无效掉期(Y& rhs){

std :: swap(msg_vec,rhs.msg_vec);

}

Y& operator =(const Y& rhs){

Y tmp(rhs);

swap(tmp);

return * this;

}

void add(msg& ms)

{msg_vec.push_back(ms); }


void print()

{

if(msg_vec.size())

std :: cout<< msg_vec [0] .get_some_str()<<的std :: ENDL; //只为

测试

}


};


类控制器{

typedef std :: vector< Y> VEC_Y;

VEC_Y vectorOfYs;

public:


Controller(){}

Controller( const Controller& rhs)

:vectorOfYs(rhs.vectorOfYs)

{}


void swap(Controller& rhs){

std :: swap(vectorOfYs,rhs.vectorOfYs);

}

Controller& operator =(const Controller& rhs){

Controller tmp(rhs);

swap(tmp);

return * this;

}


void add_y(Y myY)

{vectorOfYs.push_back(myY);}


void add_msg(msg& ms)

{

if(vectorOfYs.size())

vectorOfYs [0] .add (女士); //用于测试的蛮力。

}

void print(){

if(vectorOfYs.size())

vectorOfYs [0] .print(); //只是试验

}

};


int main()

{

控制器ctrl1;

ctrl1.add_y(Y());

ctrl1.add_msg(msg(" test"));


控制器ctrl2;

ctrl2 = ctrl1;

ctrl2.print();

}

对于两个控制器对象之间的赋值,类

控制器包含Y'的向量,而向量包含向量

msg '的;意味着theres - 可能是所有_three_类中复制

构造函数的要求。正确吗?


Consider:

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

class msg {
std::string someStr;
public:
msg(const std::string& sStr)
: someStr(sStr) {}
msg( const msg& rhs )
: someStr(rhs.someStr)
{}
void swap( msg& rhs ) {
std::swap( someStr, rhs.someStr );
}
msg& operator=( const msg& rhs) {
msg tmp (rhs);
swap(tmp);
return *this;
}
std::string get_some_str() const { return someStr; }
};

class Y {
typedef std::vector<msg> msgVec;
msgVec msg_vec;
public:
Y(){}
Y( const Y& rhs )
: msg_vec(rhs.msg_vec)
{}
void swap( Y& rhs ) {
std::swap( msg_vec, rhs.msg_vec );
}
Y& operator=( const Y& rhs) {
Y tmp (rhs);
swap(tmp);
return *this;
}
void add(msg& ms)
{ msg_vec.push_back(ms); }

void print()
{
if (msg_vec.size())
std::cout << msg_vec[0].get_some_str() << std::endl; // just for
test
}

};

class Controller {
typedef std::vector<Y> VEC_Y;
VEC_Y vectorOfYs;
public:

Controller() {}
Controller( const Controller& rhs )
: vectorOfYs(rhs.vectorOfYs)
{}

void swap( Controller& rhs ) {
std::swap( vectorOfYs, rhs.vectorOfYs );
}
Controller& operator=( const Controller& rhs) {
Controller tmp (rhs);
swap(tmp);
return *this;
}

void add_y(Y myY)
{ vectorOfYs.push_back(myY);}

void add_msg ( msg& ms )
{
if (vectorOfYs.size())
vectorOfYs[0].add(ms); // brute force for test.
}
void print() {
if (vectorOfYs.size())
vectorOfYs[0].print(); // just experimenting
}
};

int main()
{
Controller ctrl1;
ctrl1.add_y(Y());
ctrl1.add_msg(msg("test"));

Controller ctrl2;
ctrl2 = ctrl1;
ctrl2.print();
}
For assignment between two controller objects, the fact that the class
Controller contains a vector of Y''s which in turn contains a vector of
msg''s; implies that theres - perhaps a requirement for a copy
constructor in all _three_ classes. Correct?

推荐答案

ma740988写道:
ma740988 wrote:
考虑:

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

class msg {
std :: string someStr;
public:
msg(const std :: string& sStr)
:someStr(sStr){}
msg( const msg& rhs)
:someStr(rhs.someStr)
{}
void swap(msg& rhs){
std :: swap(someStr,rhs.someStr);
}
msg& operator =(const msg& rhs){
msg tmp(rhs);
swap(tmp);
return * this;


真的吗?不完全是?为什么不简单


someStr = rhs.someStr;

返回* this;




}
std :: string get_some_str()const {return someStr; }
};

类Y {
typedef std :: vector< msg> msgVec;
msgVec msg_vec;
公开:
Y(){}
Y(const Y& rhs)
:msg_vec(rhs.msg_vec)
{}
void swap(Y& rhs){
std :: swap(msg_vec,rhs.msg_vec);
}
Y& operator =(const Y& rhs){
Y tmp(rhs);
swap(tmp);


再次......为什么?只需指定:


msg_vec = rhs.msg_vec;

返回* this;
}
void add(msg& ms)


void add(msg const& ms)//好一点

{msg_vec.push_back(ms); }

void print()
{
if(msg_vec.size())
std :: cout<< msg_vec [0] .get_some_str()<<的std :: ENDL; //仅用于
测试
}

};

类控制器{
typedef std :: vector< Y> VEC_Y;
VEC_Y vectorOfYs;
公众:

控制器(){}
控制器(const Controller& rhs)
:vectorOfYs(rhs.vectorOfYs)
{}

void swap(Controller& rhs){
std :: swap(vectorOfYs,rhs.vectorOfYs);
}
控制器& operator =(const Controller& rhs){
控制器tmp(rhs);
swap(tmp);


< sigh> ...

返回* this;
}

void add_y(Y myY)


void add_y(Y const& myY)

{vectorOfYs.push_back(myY);}

void add_msg(msg& ; ms)
{
if(vectorOfYs.size())


if(!vectorOfYs.empty())//更好

vectorOfYs [0] .add(ms); //用于测试的蛮力。
}
void print(){
if(vectorOfYs.size())


if(!vectorOfYs。 empty())//更好

vectorOfYs [0] .print(); //只是试验
}
};

int main()
{
控制器ctrl1;
ctrl1.add_y(Y() );
ctrl1.add_msg(msg(" test"));

控制器ctrl2;
ctrl2 = ctrl1;
ctrl2.print();
}

对于两个控制器对象之间的赋值,类
控制器包含一个Y'向量的事实,而该向量又包含一个
msg''的向量S;意味着theres - 可能是所有_three_类中复制
构造函数的要求。正确?
Consider:

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

class msg {
std::string someStr;
public:
msg(const std::string& sStr)
: someStr(sStr) {}
msg( const msg& rhs )
: someStr(rhs.someStr)
{}
void swap( msg& rhs ) {
std::swap( someStr, rhs.someStr );
}
msg& operator=( const msg& rhs) {
msg tmp (rhs);
swap(tmp);
return *this;
Really? No, REALLY? Why not simply

someStr = rhs.someStr;
return *this;

?
}
std::string get_some_str() const { return someStr; }
};

class Y {
typedef std::vector<msg> msgVec;
msgVec msg_vec;
public:
Y(){}
Y( const Y& rhs )
: msg_vec(rhs.msg_vec)
{}
void swap( Y& rhs ) {
std::swap( msg_vec, rhs.msg_vec );
}
Y& operator=( const Y& rhs) {
Y tmp (rhs);
swap(tmp);
Again... WHY? Just assign:

msg_vec = rhs.msg_vec;
return *this;
}
void add(msg& ms)
void add(msg const & ms) // a bit better
{ msg_vec.push_back(ms); }

void print()
{
if (msg_vec.size())
std::cout << msg_vec[0].get_some_str() << std::endl; // just for
test
}

};

class Controller {
typedef std::vector<Y> VEC_Y;
VEC_Y vectorOfYs;
public:

Controller() {}
Controller( const Controller& rhs )
: vectorOfYs(rhs.vectorOfYs)
{}

void swap( Controller& rhs ) {
std::swap( vectorOfYs, rhs.vectorOfYs );
}
Controller& operator=( const Controller& rhs) {
Controller tmp (rhs);
swap(tmp);
<sigh>...
return *this;
}

void add_y(Y myY)
void add_y(Y const & myY)
{ vectorOfYs.push_back(myY);}

void add_msg ( msg& ms )
{
if (vectorOfYs.size())
if (!vectorOfYs.empty()) // better
vectorOfYs[0].add(ms); // brute force for test.
}
void print() {
if (vectorOfYs.size())
if (!vectorOfYs.empty()) // better
vectorOfYs[0].print(); // just experimenting
}
};

int main()
{
Controller ctrl1;
ctrl1.add_y(Y());
ctrl1.add_msg(msg("test"));

Controller ctrl2;
ctrl2 = ctrl1;
ctrl2.print();
}
For assignment between two controller objects, the fact that the class
Controller contains a vector of Y''s which in turn contains a vector of
msg''s; implies that theres - perhaps a requirement for a copy
constructor in all _three_ classes. Correct?




不,不是真的。由于这些对象在复制构造期间都不需要任何特殊的处理,因此编译器生成的一个

恰到好处。


另外,为什么你在任务中做了所有这些废话

运营商?只需指定该死的成员,然后继续更多

重要的事情...


V



No, not really. Since none of those objects require any special
processing during copy-construction, the compiler-generated one
is just right.

Besides, why are you doing all this nonsense in the assignment
operators? Simply assign the damn members and move on to more
important things...

V


Victor Bazarov <五******** @ comAcast.net>在消息中写道

新闻:a4 ****************************** @ comcast.com。 ..
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:a4******************************@comcast.com. ..
ma740988写道:
ma740988 wrote:
考虑:

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

class msg {
std :: string someStr;
public:
msg(const std :: string& sStr)
:someStr(sStr){}
msg(const msg& rhs)
:someStr(rhs.someStr)
{}
void swap(msg& rhs){
std :: swap(someStr,rhs.someStr);
}
msg& operator =(const msg& rhs){
msg tmp(rhs);
swap(tmp);
return * this;
真的吗?不完全是?为什么不简单

someStr = rhs.someStr;
返回* this;
Consider:

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

class msg {
std::string someStr;
public:
msg(const std::string& sStr)
: someStr(sStr) {}
msg( const msg& rhs )
: someStr(rhs.someStr)
{}
void swap( msg& rhs ) {
std::swap( someStr, rhs.someStr );
}
msg& operator=( const msg& rhs) {
msg tmp (rhs);
swap(tmp);
return *this;
Really? No, REALLY? Why not simply

someStr = rhs.someStr;
return *this;




更进一步,为什么定义operator = at all ?定义运算符=不必要地

会导致忘记分配可能在

未来添加的任何成员的风险。忘记分配基类是我过去生活的另一个问题。


[...]

不,不是真的。由于这些对象在复制构造期间都不需要任何特殊的处理,因此编译器生成的对象恰好是正确的。


同意......我想添加operator =;因为它应该得到相同的

处理作为这里的复制构造函数:)

此外,你为什么要在作业中做所有这些废话
运算符?简单地指定该死的成员并继续进行更多重要的事情......



Even further, why define operator= at all? Defining operator= unnecessarily
opens the risk of forgetting to assign any members that may be added in the
future. Forgetting to assign the base classes is another problem that I''ve
lived in the past.

[...]
No, not really. Since none of those objects require any special
processing during copy-construction, the compiler-generated one
is just right.
Agreed... I wanted to add operator=; because it should get the same
treatment as the copy constructor here :)
Besides, why are you doing all this nonsense in the assignment
operators? Simply assign the damn members and move on to more
important things...




现在我开始怀疑可能有一些特别的事情。 />
operator =。你同意编译器生成的operator =恰到好处,

对吗? :)


Ali



Now I am beginning to suspect that there may be something special about
operator=. You agree that the compiler generated operator= is just right,
right? :)

Ali


Victor Bazarov写道:
Victor Bazarov wrote:
[... ]

此外,你为什么要在作业中做所有这些废话?
运营商?简单地指定该死的成员并转向更重要的事情...
[...]

Besides, why are you doing all this nonsense in the assignment
operators? Simply assign the damn members and move on to more
important things...




引用Sutter和Alexandrescu:

" ;在

赋值运算符中使用交换函数通常很有用。下面提到的实现给了

强大的可靠性,尽管以构建一个

额外对象为代价。如果速度很重要,则不应使用


T& T :: operator =(const T& other){

T temp(other);

swap(temp);

return * this ;

}


所以,如果我理解正确的话,这意味着如果你交换操作并且副本ctor不可用,那就是

(他们不会抛出b $ b)这使得你的任务操作员也不可能。


那就是说,我认为称之为胡说八道。相当坚定。

措辞强烈。


有人纠正我,如果我错了 - 我只读了这本书

last一周:)


欢呼,

- J.



To quote Sutter and Alexandrescu:
"It is often beneficient to use the swap function in the
assignment operator. The below mentioned implementation gives
strong reliability, although at the price of constructing an
extra object. If speed matters it should not be used"

T& T::operator=(const T& other) {
T temp(other);
swap(temp);
return *this;
}

So, if I understand correctly it means that if you make
the swap operation and the copy ctor unfailible (they don''t
throw) that makes your assignment operator unfailible too.

That said, I think calling it "nonsense" is rather
strong worded.

Someone correct me if I''m wrong -- I only read the book
last week :)

cheers,
- J.


这篇关于关于赋值运算符的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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