boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 【讨论】为什么要用友员来实现算符重载?
相关主题
C++糟粕和需要避免的。
IBM高级软件工程师老印的示例代码,大家看看有多少个bug? (
重载 ^ 操作符编译出错
能否对某个库进行操作符重载?
请问关于overloading <<
有没有会自动聚合的操作符重载或宏?
请问以下代码有什么错误?
请问c++中操作符可以声明为虚函数吗?
基本功不扎实,问个问题
大侠进来看看这个问题
相关话题的讨论汇总
话题: complex话题: 算符话题: 重载话题: double话题: const
进入Programming版参与讨论
1 (共1页)
f********a
发帖数: 1109
1
算符重载很方便,比方说:
class complex
{
public:
complex(double r, double i){
real = r, imag = i;}
complex operator +(const complex & c);//算符重载
};
为什么有人用友员来实现算符重载, 有什么好处?
class complex
{
public:
complex(double r, double i){
real = r, imag = i;}
friend complex operator +(const complex &c1, const complex &c2);//友员
};
查了好多书都没有提到有什么好处。。
f********a
发帖数: 1109
2
前面一种用法 A = B + C;
可以阿,编译通过的。
o******r
发帖数: 259
3
Sorry,记错了,
当需要access两个class的 private data时用friend function.
另外,当操作符左边是class object的时候, 才会调用class member,
否则用friend function,
比如2个String相加,
String myStr;
"abcd" + myStr 最好定义friend function

【在 f********a 的大作中提到】
: 前面一种用法 A = B + C;
: 可以阿,编译通过的。

p****o
发帖数: 1340
4
friend functions can also take advantage of implicit conversion. but
it is arguable whether this is good or not.

【在 o******r 的大作中提到】
: Sorry,记错了,
: 当需要access两个class的 private data时用friend function.
: 另外,当操作符左边是class object的时候, 才会调用class member,
: 否则用friend function,
: 比如2个String相加,
: String myStr;
: "abcd" + myStr 最好定义friend function

1 (共1页)
进入Programming版参与讨论
相关主题
大侠进来看看这个问题
Why should i include .cpp instead of .h
[合集] 请问-fno-implicit-templates的用处
question about const reference
[合集] static const代替define的performance tradeoff在哪里?
const_cast问题
function declaration
急!问个有关aligment的问题。
VB中怎么产生一个有特殊ASCII字符的string
c# question: char_array.ToString() is not working ?
相关话题的讨论汇总
话题: complex话题: 算符话题: 重载话题: double话题: const