注意:答案是按照特定的顺序给出的,但由于许多用户根据投票而不是给出的时间对答案进行排序,因此这里是答案的索引,它们是最有意义的顺序:
(注意:这是Stack Overflow的C++常见问题解答的一个条目.如果你想批评在这种形式下提供常见问题解答的想法,那么发布所有这些的元数据的发布将是这样做的地方.这个问题在C++聊天室中受到监控,其中FAQ的想法一开始就出现了,所以你的答案很可能被那些提出想法的人阅读.)
我对它们之间的差异感到有点困惑
Type operator + (const Type &type);
Type &operator += (const Type &type);
Run Code Online (Sandbox Code Playgroud)
和
friend Type operator + (const Type &type1, const Type &type2);
friend Type &operator += (const Type &type1, const Type &type2);
Run Code Online (Sandbox Code Playgroud)
哪种方式是首选,它们看起来像什么,什么时候应该使用?
我有自己的类,我定义并使用<<运算符,如下所示:
vw& vw::operator<<(const int &a) {
// add a to an internal buffer
// do some work when it's the last number
return *this;
}
...
vw inst;
inst << a << b << c << d;
...
inst << a << b;
...
Run Code Online (Sandbox Code Playgroud)
链<<的数量每次都不同.这些数字一起代表一个代码,我需要在代码完成时做一些事情.
我还有其他选择可以知道它何时完成,而不是为每个链添加一个特殊的终止值,如下所示?
inst << a << b << c << d << term;
...
inst << a << b << term;
Run Code Online (Sandbox Code Playgroud)
EDIT2:遵循LogicStuff的当前解决方案:
--- chain.h ---
#pragma once
#include <iostream>
class chain
{
public: …Run Code Online (Sandbox Code Playgroud) 注意:我认为这在技术上与这个问题重复,但是:
==相当激进,我不确定恢复 9 年的问题是否是正确的做法。==符。<=><ps 我目前有自己的看法(基于 foonathan 的一些谈话),但这只是当前的偏好,我不希望以此来偏见潜在的答案。
我有一个班级桑普.在Samp.cpp中,我可以定义/声明一个类似的函数
Samp& operator+(Samp& other) {
std::cout << "something";
return other;
}
Run Code Online (Sandbox Code Playgroud)
这个功能究竟是什么?我怎么称呼它?
我对技术物流很感兴趣.是否有任何优势,例如保存的内存等,来实现处理类的某些功能?
特别是,将操作符重载实现为自由函数(假设您不需要访问任何私有成员,即使这样,您也可以让他们使用非成员的朋友)?
每次创建对象时,是否为类的每个函数提供了不同的内存地址?
我一直在梳理互联网以找到答案,但找不到任何答案。给出的唯一原因似乎与比较不同类型的对象(例如MyClass == int)有关。但是最常见的用例是将一个类实例与同一类的另一个实例进行比较,而不是与任何不相关的类型进行比较。
换句话说,我确实了解以下问题:
struct A {
bool operator==(int b);
};
Run Code Online (Sandbox Code Playgroud)
但是我找不到最明显的用例不使用成员函数的任何充分理由:
struct A {
bool operator==(const A&);
};
Run Code Online (Sandbox Code Playgroud)
另一方面,成员超载似乎有几个积极方面:
将重载operator==作为非成员函数仅仅是为了保持其与其他类中可能的重载相同的约定?还是有其他原因使其成为非会员?
当我尝试添加bool operator==(complx a, complx b)时说
|Code |Description
|EO344 too many parameters for this operator function
Run Code Online (Sandbox Code Playgroud)
我尝试仅使用一个参数,但无法执行。
这是实际功能:
//boolean operators "|Code |Description
bool operator==(complx a, complx b) //error: " EO344 too many parameters for this operator function"
{
/*error code*/return a.real() == b.real() && a.imag() == b.imag();
}
Run Code Online (Sandbox Code Playgroud)
它说参数太多,但我的书说没有。你能帮助我吗?
我正在尝试重载<<运算符,但我得到一个错误,低于.
rollingDice.h|14|error: ‘std::ostream& rollingDice::operator<<(std::ostream&, const rollingDice&)’ must take exactly one argument|
Run Code Online (Sandbox Code Playgroud)
这是我的代码.我将执行和删除分开了.我认为问题的出现是因为我的编码和很多网页和Deitel&Deitel一样.
rollingDice.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
#include "rollingDice.h"
using namespace std;
rollingDice::rollingDice(unsigned int valN)
{
n=valN;
r=new int [n];
}
int rollingDice::length()
{
return n;
}
void rollingDice::generate()
{
srand48(time(NULL));
int i=0;
for (i=0; i<n; ++i)
{
r[i]=1+(lrand48()%6);
}
}
rollingDice& rollingDice::init(unsigned int valN)
{
n=valN;
r=new int [n];
return *this;
}
ostream& operator << (ostream& output, rollingDice& rd)
{
int temp=n;
if (temp>12)
temp=12; …Run Code Online (Sandbox Code Playgroud) 我在很多地方读过这篇文章,建议在重载运算符时使用“friend”,但没有人清楚地解释为什么真正需要它?为什么我们不能将它们声明为普通成员函数?有什么缺点吗?
谷歌搜索但没有得到任何明确的答案。
我想使用一个类作为std :: map中的键。
std::map<Type, value> collection;
Run Code Online (Sandbox Code Playgroud)
尽管我定义了operator<,但密钥不被接受:Invalid operands to binary expression ('const Type' and 'const Type')。
class Type {
public:
inline bool operator< (const Type& rhs) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
为什么会这样呢?