简体   繁体   English

如何在c ++中重载赋值运算符

[英]How can I overload the assignment operator in c++

Ive been reading this article: https://www.thc.org/root/phun/unmaintain.html , and Ive been trying to implements steps Misleading names (search for isValid(x) ) and How To Fix Unused Variable Errors (#29 under Miscellaneous Techniques). 我一直在阅读这篇文章: https//www.thc.org/root/phun/unmaintain.html ,我一直在尝试实现Misleading names (搜索isValid(x) )以及How To Fix Unused Variable Errors (#杂项技术下的29)。

I wish to do something more useful, (also according to the article) as doing more stuff on less lines is better. 我希望做一些更有用的东西(也是根据文章),因为在更少的线上做更多的东西会更好。

Unfortunately, I have no idea on how to overload the assignment operator for a basic type. 不幸的是,我不知道如何为基本类型重载赋值运算符。 I know I could do something like operator=(&Other o) if I was using a class, but alas, C/C++ doesn't take after Java by having classes for some basic types... 我知道如果我使用的是类,我可以做类似operator=(&Other o) ,但是,唉,C / C ++不会因为某些基本类型的类而需要Java之后...

Unfortunately, I have no idea on how to overload the assignment operator for a basic type. 不幸的是,我不知道如何为基本类型重载赋值运算符。 I know I could do something like operator=(&Other o) if I was using a class 我知道如果我使用的是类,我可以做类似operator=(&Other o)

You cannot overload operators for built-in types in C++. 您不能在C ++中为内置类型重载运算符。 And that's the end of the story. 这就是故事的结局。

but alas, C/C++ doesn't take after Java by having classes for some basic types... 但是,C / C ++并没有采用Java来为一些基本类型提供类...

...You make it sound like Java's distinction between int and Integer is somehow a good thing :) ...你听起来像Java在intInteger之间的区别在某种程度上是一件好事:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM