删除const以使赋值运算符受益 [英] Removing const for benefit of assignment operator

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

问题描述

我是否应该为了

赋值运算符而从私有成员中删除const?


我有一个看起来像这样的类。


class Foo

{

public:

Foo(const std :: string& description ):_ description(description){}

private:

const std :: string _description;

};


编译器警告我它不能生成赋值运算符,因为_description是const,所以
。除非你将
分配给Foo实例的新对象,否则_description永远不会改变。这是否需要制作

_description non-const。


不知怎的,我看到oldFoo = newFoo更多的是为
$ b $指定一个新对象b oldFoo-identifier而不是更改oldFoo-object。这是一个糟糕的把握

概念在我身边?


提前致谢。


-

Joost Ronkes Agerbeek

黄木工作室
http://www.yellowwoodstudios.com/
http:/ /www.ronkes.nl/

Should I remove const from a private member just for the sake of the
assignment operator?

I have a class that looks something like this.

class Foo
{
public:
Foo(const std::string& description) : _description(description) {}

private:
const std::string _description;
};

The compiler warns me that it cannot generate an assignment operator,
because _description is const. _description will never change unless you
assign a new object to the Foo-instance. Does that warrant making
_description non-const.

Somehow I see oldFoo = newFoo more as assigning a new object to the
oldFoo-identifier than as changing the oldFoo-object. Is that a poor grasp
of concept on my side?

Thanks in advance.

--
Joost Ronkes Agerbeek
Yellow Wood Studios
http://www.yellowwoodstudios.com/
http://www.ronkes.nl/

推荐答案



" Joost Ronkes Agerbeek" <乔*** @ ronkes.nl>在消息中写道

news:41 *********************** @ news.xs4all.nl ...

"Joost Ronkes Agerbeek" <jo***@ronkes.nl> wrote in message
news:41***********************@news.xs4all.nl...
我是否应该为了
赋值运算符而从私有成员中删除const?

我有一个看起来像这样的类。
Foo
{公开:
Foo(const std :: string& description):_ description(description){}

private:
const std :: string _description;
};

编译器警告我它无法生成赋值运算符,因为_description是const。除非您将新对象分配给Foo实例,否则_description永远不会更改。这是否有必要使
_description非const.

不知怎的,我认为oldFoo = newFoo更像是为
oldFoo-identifier分配一个新对象而不是更改oldFoo-object。难道我的概念很难掌握吗?


是的。 :-)赋值是复制操作。赋值操作符需要

从右侧对象复制到对象的每个成员,并且

在这种情况下它不能这样做,因为string是const。如果你要完全允许分配你的对象,那么你必须能够改变_description。 (正如你所说,如果你指定它,它会改变。)

因此,它不应该是常数。


-Howard

提前致谢。

- Joost Ronkes Agerbeek
黄木工作室
http://www.yellowwoodstudios.com/
http://www.ronkes.nl/
Should I remove const from a private member just for the sake of the
assignment operator?

I have a class that looks something like this.

class Foo
{
public:
Foo(const std::string& description) : _description(description) {}

private:
const std::string _description;
};

The compiler warns me that it cannot generate an assignment operator,
because _description is const. _description will never change unless you
assign a new object to the Foo-instance. Does that warrant making
_description non-const.

Somehow I see oldFoo = newFoo more as assigning a new object to the
oldFoo-identifier than as changing the oldFoo-object. Is that a poor grasp
of concept on my side?

Yes. :-) An assignment is a copy operation. The assignment operator needs
to copy into each member of your object from the right-hand-side object, and
it can''t do that in this case because the string is const. If you''re going
to allow assignment to your object at all, then you''ll have to be able to
change _description. (As you said, it WILL change if you assign to it.)
Thus, it shouldn''t be const.

-Howard
Thanks in advance.

--
Joost Ronkes Agerbeek
Yellow Wood Studios
http://www.yellowwoodstudios.com/
http://www.ronkes.nl/



Joost Ronkes Agerbeek写道:
Joost Ronkes Agerbeek wrote:
我是否应该为了赋值运算符而从私有成员中删除const?

我有一个看起来类似的类这个。

类Foo {
公共:
Foo(const std :: string& description):_ description(描述){}
私人:
const std :: string _description;
};

编译器警告我它无法生成赋值运算符,
因为_descrip是const。除非你为Foo实例分配一个新对象,否则_description将永远不会改变。
这是否保证使_description非const。
cat Foo.cc
#include< string>

#include< iostream>


class Foo {

私人:

const std :: string _description;

public:

朋友

std :: ostream的和放; operator<<(std :: ostream& os,const Foo& f){

return os<< f._description;

}

Foo& operator =(const Foo& f){return * this; } $ / $
Foo(const std :: string& description):

_description(description){}

};


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

Foo a(std :: string(" a"));

std :: cout<< a<< std :: endl;

a = Foo(std :: string(" b"));

std :: cout<< a<< std :: endl;

返回0;

}

g ++ -Wall -ansi -pedantic -o Foo Foo.cc
./Foo
a

a

不知怎的,我看到oldFoo = newFoo更像是为
oldFoo-identifier分配一个新对象而不是改变oldFoo-对象。
对我这个概念的把握很差吗?
Should I remove const from a private member
just for the sake of the assignment operator?

I have a class that looks something like this.

class Foo {
public:
Foo(const std::string& description): _description(description) { }
private:
const std::string _description;
};

The compiler warns me that it cannot generate an assignment operator,
because _description is const. _description will never change
unless you assign a new object to the Foo-instance.
Does that warrant making _description non-const. cat Foo.cc #include <string>
#include <iostream>

class Foo {
private:
const std::string _description;
public:
friend
std::ostream& operator<<(std::ostream& os, const Foo& f) {
return os << f._description;
}
Foo& operator=(const Foo& f) { return *this; }
Foo(const std::string& description):
_description(description) { }
};

int main(int argc, char* argv[]) {
Foo a(std::string("a"));
std::cout << a << std::endl;
a = Foo(std::string("b"));
std::cout << a << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o Foo Foo.cc
./Foo a
a

Somehow I see oldFoo = newFoo more as assigning a new object to the
oldFoo-identifier than as changing the oldFoo-object.
Is that a poor grasp of concept on my side?




是的。



Yes.




E。 Robert Tisdale <,E ************** @ jpl.nasa.gov>。在消息中写道

news:co ********** @ nntp1.jpl.nasa.gov ...

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:co**********@nntp1.jpl.nasa.gov...
Joost Ronkes Agerbeek写道:
Joost Ronkes Agerbeek wrote:
我是否应该为了赋值运算符而从私有成员中删除const?

我有一个看起来像这样的类。类Foo {
公共:
Foo(const std :: string& description):_ description(description){}
private:
const std: :string _description;
};

编译器警告我它无法生成赋值运算符,因为_description是const。除非你为Foo实例分配一个新对象,否则_description永远不会改变

这是否保证_description非常量。
Should I remove const from a private member
just for the sake of the assignment operator?

I have a class that looks something like this.

class Foo {
public:
Foo(const std::string& description): _description(description) { }
private:
const std::string _description;
};

The compiler warns me that it cannot generate an assignment operator,
because _description is const. _description will never change
unless you assign a new object to the Foo-instance.
Does that warrant making _description non-const.
> cat Foo.cc
> cat Foo.cc


#include< string>
#include< iostream>

class Foo {
private:
const std :: string _description;
公众:
朋友
std :: ostream& operator<<(std :: ostream& os,const Foo& f){
return os<< f._description;
}
Foo& operator =(const Foo& f){return * this; }


#include <string>
#include <iostream>

class Foo {
private:
const std::string _description;
public:
friend
std::ostream& operator<<(std::ostream& os, const Foo& f) {
return os << f._description;
}
Foo& operator=(const Foo& f) { return *this; }




这有什么用呢?这里没有任何任务。事实上,

f完全被忽略了!我不认为那是他想要的......你呢?

Foo(const std :: string& description):
_description(description){}
};

int main(int argc,char * argv []){
Foo a(std :: string(" a"));
std: :cout<< a<< std :: endl;
a = Foo(std :: string(" b"));


但是因为operator =只是返回*这个,这只是一个本身,怎么可以这个行构成一个临时的赋值?变量a

在这里根本不应该改变,对吧?

std :: cout<< a<< std :: endl;
返回0;
}



How does this help anything? No assignment is taking place here. In fact,
f is completely ignored! I don''t think that''s what he wanted... do you?
Foo(const std::string& description):
_description(description) { }
};

int main(int argc, char* argv[]) {
Foo a(std::string("a"));
std::cout << a << std::endl;
a = Foo(std::string("b"));
But since operator= simply returns *this, which is just a itself, how can
this line constitute an assignment of the temporary to a? The variable a
shouldn''t change at all here, right?
std::cout << a << std::endl;
return 0;
}

> g ++ -Wall -ansi -pedantic -o Foo Foo.cc
> ./Foo
> g++ -Wall -ansi -pedantic -o Foo Foo.cc
> ./Foo


a


a
a


不知怎的,我看到oldFoo = newFoo更像是为
oldFoo-identifier比改变oldFoo对象。对我这个概念的掌握是否很差?

Somehow I see oldFoo = newFoo more as assigning a new object to the
oldFoo-identifier than as changing the oldFoo-object. Is that a poor
grasp of concept on my side?



是的。



Yes.



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

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