相关疑难解决方法(0)

运算符重载的基本规则和习惯用法是什么?

注意:答案是按照特定的顺序给出的,但由于许多用户根据投票而不是给出的时间对答案进行排序,因此这里是答案索引,它们是最有意义的顺序:

(注意:这是Stack Overflow的C++常见问题解答的一个条目.如果你想批评在这种形式下提供常见问题解答的想法,那么发布所有这些的元数据的发布将是这样做的地方.这个问题在C++聊天室中受到监控,其中FAQ的想法一开始就出现了,所以你的答案很可能被那些提出想法的人阅读.)

c++ operator-overloading operators c++-faq

2074
推荐指数
8
解决办法
88万
查看次数

为什么在类之外定义operator +或+ =,以及如何正确地执行它?

我对它们之间的差异感到有点困惑

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)

哪种方式是首选,它们看起来像什么,什么时候应该使用?

c++ operator-overloading operators

27
推荐指数
2
解决办法
2万
查看次数

如何知道链自定义运算符<<调用的数量?

我有自己的类,我定义并使用<<运算符,如下所示:

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)

c++ visual-c++

7
推荐指数
2
解决办法
214
查看次数

C++ 20 中的运算符 == 和 &lt;=&gt; 应该作为成员函数还是自由函数实现?

注意:我认为这在技术上与这个问题重复,但是:

  1. C++20 中的更改==相当激进,我不确定恢复 9 年的问题是否是正确的做法。
  2. 我具体询问了编译器正在重写的运算符,而不是例如运算==符。<=><

ps 我目前有自己的看法(基于 foonathan 的一些谈话),但这只是当前的偏好,我不希望以此来偏见潜在的答案。

c++ spaceship-operator c++20

7
推荐指数
1
解决办法
1285
查看次数

在C++中声明一个operator +函数

我有一个班级桑普.在Samp.cpp中,我可以定义/声明一个类似的函数

 Samp& operator+(Samp& other) {
  std::cout << "something";
  return other;
}
Run Code Online (Sandbox Code Playgroud)

这个功能究竟是什么?我怎么称呼它?

c++ operator-overloading

6
推荐指数
1
解决办法
156
查看次数

将函数实现为自由函数而不是C++中的成员是否有任何优势?

我对技术物流很感兴趣.是否有任何优势,例如保存的内存等,来实现处理类的某些功能?

特别是,将操作符重载实现为自由函数(假设您不需要访问任何私有成员,即使这样,您也可以让他们使用非成员的朋友)?

每次创建对象时,是否为类的每个函数提供了不同的内存地址?

c++ optimization class c++11

6
推荐指数
2
解决办法
1037
查看次数

仅与类的其他对象进行比较时,是否有任何理由不重载operator ==作为成员?

我一直在梳理互联网以找到答案,但找不到任何答案。给出的唯一原因似乎与比较不同类型的对象(例如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)

另一方面,成员超载似乎有几个积极方面:

  • 无需成为函数的朋友或为成员提供获取器
  • 它始终可供班级用户使用(尽管这也可能是不利的一面)
  • 查找没有问题(出于某种原因,这在我们的GoogleTests中似乎很常见)

将重载operator==作为非成员函数仅仅是为了保持其与其他类中可能的重载相同的约定?还是有其他原因使其成为非会员?

c++ operator-overloading

5
推荐指数
2
解决办法
128
查看次数

我不能在类中的布尔运算符中添加两个或多个参数

当我尝试添加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)

它说参数太多,但我的书说没有。你能帮助我吗?

c++ visual-c++ c++11

2
推荐指数
1
解决办法
54
查看次数

重载<<运算符,分隔执行和删除

我正在尝试重载<<运算符,但我得到一个错误,低于.

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)

c++ operator-overloading

1
推荐指数
1
解决办法
49
查看次数

为什么建议在运算符重载中将函数声明为“友元”

我在很多地方读过这篇文章,建议在重载运算符时使用“friend”,但没有人清楚地解释为什么真正需要它?为什么我们不能将它们声明为普通成员函数?有什么缺点吗?

谷歌搜索但没有得到任何明确的答案。

c++ c++11

1
推荐指数
1
解决办法
3735
查看次数

std :: map中的用户定义类:二进制表达式的无效操作数

我想使用一个类作为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)

为什么会这样呢?

c++ c++11

0
推荐指数
1
解决办法
978
查看次数