标签: operator-overloading

C++ 是否保证 lambda 未命名类始终定义“operator bool()”?

#include <type_traits>\n\nint main()\n{\n    auto f = [] {};\n    static_assert(std::is_same_v<decltype(!f), bool>); // ok\n\n    f.operator bool(); // error: \xe2\x80\x98struct main()::<lambda()>\xe2\x80\x99 \n                       // has no member named \xe2\x80\x98operator bool\xe2\x80\x99\n}\n
Run Code Online (Sandbox Code Playgroud)\n

C++ 是否保证 lambda 未命名类始终具有已operator bool()定义的?

\n

c++ lambda standards operator-overloading c++14

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

错误:与“operator&lt;”不匹配(操作数类型为“const A”和“const A”)

#include <set>\n#include <iostream>\nusing namespace std;\n\ntemplate<class T> \nclass A \n{   \n    public:\n        A(T a = 0,T b =0): m_a(a),m_b(b) {}\n\n        bool operator<(const A& lhs)\n        {\n            /* compare code */\n        }\n              \n    private:\n        T m_a;\n        T m_b;   \n};  \n\nint main()   \n{\n    A<int> abc(2,3);\n\n    set<A<int>> P2D;\n    P2D.insert(abc);\n    return 0;   \n}\n
Run Code Online (Sandbox Code Playgroud)\n

当我运行此代码时,出现以下错误

\n
\n

操作数类型为 \xe2\x80\x98const A\xe2\x80\x99 和 \xe2\x80\x98const A\xe2\x80\x99

\n
\n

const如果我在重载上声明关键字operator<

\n
bool operator<(const A& lhs) const\n{\n    /*  compare code */\n}\n
Run Code Online (Sandbox Code Playgroud)\n

它没有给出错误,但是:

\n
    \n
  1. 我在这里缺少什么吗?
  2. \n
  3. 如果我不声明关键字,为什么会出错 …

c++ oop constants operator-overloading set

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

C++ 中 [] 的可变参数运算符重载

我希望使用运算符 [] 中传递的表达式。我认为使用变量模板参数可以解决问题,但我错了......在 c++11 中是否有一种方法可以做到这一点?

class object {
 

private:


public:
  void values() { std::cout << "finished" << std::endl; }
  template <typename T, typename... Type> void values(T arg, Type... args) {

    std::cout << arg << "  " << std::endl;
    values(args...);
  }


   template<typename... Type> void operator[](Type... args) {

       values(args...);
  }
};

int main(void) {
  object o1 = object();

  o1.values(1, 6.2, true, "hello"); // Works fine.
  
  o1[1, 6.2, true]; // Only the last value gets printed eg. true
  

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

更广泛的目标是我被要求为此制定一个工作语法

let …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading c++11

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

具有重载 less 运算符的 std::max 无法编译

这是代码:

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <utility>

struct student
{
    std::string full_name;
    int group;
    int number;

    friend std::istream& operator>>(std::istream& in, student& obj)
    {
        in >> obj.full_name >> obj.group >> obj.number;
        return in;
    }
    friend std::ostream& operator<<(std::ostream& out, const student& obj)
    {
        out << "Full name: " << obj.full_name << '\n';
        out << "Group: " << obj.group << '\n';
        out << "Number: " << obj.number << '\n';
        return out;
    }

    bool operator<(const student& obj)
    {
        return this->number …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-overloading std max

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

为什么“operator&lt;&lt;()”重载定义中需要“friend”?

class Train{
public: 
    char direction;
    int loading_time, crossing_time;        
    ...

    friend std::ostream& operator<<(std::ostream& os, const Train& t){
        os << t.direction << '/' << t.loading_time << '/' << t.crossing_time;
        return os;
    }
};
Run Code Online (Sandbox Code Playgroud)

为什么在这种情况下需要“朋友”?所有属性都是公共的。我应该只使用结构体吗?

c++ class operator-overloading friend

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

C++ 中是否有与 Python 的 __rmul__ 等效的函数?

Python 3 允许重载*.

例如,

a*b

允许a通过提供方法来定义结果__mul__

然而,如果该方法返回,则调用NotImplemented,__rmul__方法b(如果存在)以给出结果。此处记录了这一点。

这样做的一个优点是我可以在ba知道a.

C++ 中是否有等效的方法__rmul__,或者是否有实现相同或类似事物的方法?

c++ overloading operator-overloading

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

您能否避免数学运算符的多个运算符重载?

假设我有一个非常简单的 Rational 类,如下所示:

class Rational
{

    int numer;
    int denom;

public:

    Rational(const int& numer, const int& denom) : numer(numer), denom(denom) {}
    void operator*(const Rational& other) { std::cout << "multiply, simple as\n"; }
}
Run Code Online (Sandbox Code Playgroud)

一切都很好。然后说我希望能够将 Rational 类与整数相乘,因此我向该类添加另一个函数:

class Rational
{
    
    int numer;
    int denom;
    
public:
    
    Rational(const int& numer, const int& denom) : numer(numer), denom(denom) {}
    void operator*(const Rational& other) { std::cout << "multiply, simple as\n"; }
    void operator*(const int& other) { std::cout << "some math here\n"; }
}
Run Code Online (Sandbox Code Playgroud)

好吧,没什么大不了的。除了我实际上无法执行以下操作,因为参数的顺序都是错误的:

Rational mine(1, …
Run Code Online (Sandbox Code Playgroud)

c++ overloading class operator-overloading

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

为什么重载new和delete运算符时释放的内存小于分配的内存?

我创建了一个类并重载了newdelete运算符来打印分配/释放的内存大小。在下面的示例中,它分配了 28 个字节,但释放了 4 个字节。为什么?

#include <iostream>
#include <string>
using namespace std;

class Person
{
private:
    string name;
    int age;

public:
    Person() {
        cout << "Constructor is called.\n";
    }

    Person(string name, int age) {
        this->name = name;
        this->age = age;
    }

    void* operator new(size_t size) {
        void* p = malloc(size);
        cout << "Allocate " << size << " bytes.\n";
        return p;
    }

    void operator delete(void* p) {
        free(p);
        cout << "Free " << sizeof(p) << " …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading new-operator delete-operator

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

重载 &lt;&lt; 运算符时出现错误

我有一个名为“Vector”的类。它由两个私有字段组成:std::vector<double> coordinatesint len。方法dim()返回len

<<像这样重载运算符:

friend std::ostream& operator<<(std::ostream& os,  Vector& vec ) 
{
    std:: cout << "(";
    for ( int i = 0; i < vec.dim(); i++ ) {
        if ( i != vec.dim()-1){
            os << vec[i] << ", ";
        } else {
            os << vec[i];
        }
    }
    os << ')';
    return os;
}
Run Code Online (Sandbox Code Playgroud)

+像这样的运算符:

friend Vector operator +(Vector& first, Vector& second)
{
    if(first.dim() != second.dim()){
        throw std::length_error{"Vectors must be …
Run Code Online (Sandbox Code Playgroud)

c++ overloading vector operator-overloading

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

关于C++模板类中重载运算符T()的问题

我有这么一小段代码:

template<typename T>
class Test
{
public:
    //operator T() const { return toto; }
    T toto{ nullptr };
};

void function(int* a) {}

int main(int argc, char** argv)
{
    Test<int*> a;
    function(a);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

除非该行未被注释,否则它不会编译operator T() const { return toto; }。这神奇地起作用,但我不确定为什么(如果我取消注释该行)。

a我确实理解,如果该行被注释,则传递给的类型function()与预期的类型不兼容int*。所以,编译器当然会抱怨……没问题。

我还了解运算符返回对象的实际类型,因此在这种特殊情况下编译器很高兴。

我不明白为什么在这种特殊情况下调用操作员。

做与function(a)做同样的事情function(a()),只是()隐式的吗?

c++ operator-overloading

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