标签: operator-overloading

方法重载Python

这类似于方法重载。当print Hello(1, 2, 3)执行时它会返回"a and b",而我希望它返回"a and b and c"。我知道我可以说if (a and b and c is None)这样就会有效。但是,如果我有 20 个参数并且我必须处理每种情况,那么这将只是多个if我认为没有必要的语句。我应该有更好的方法来解决这样的问题吗?

 def Hello(a=None, b=None, c=None):
    if(a and b):
        return "a and b"
    if(a and c):
       return "a and c"
    if(a and b and c):
       return "a and b and c"

print Hello(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

python operator-overloading

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

是否可以覆盖 Scala 中的内置运算符?

这是一个假设性的问题,但是假设我想+在一个范围内改变(或任何其他算术运算符)的行为Int,就像这样(我知道这是疯狂的事情,是我试图避免的事情)一般来说,但我觉得很有趣):

object MySillyStuff extends App {
  def +(a: Int, b: Int) = a*b;

  println(1+2)
}
Run Code Online (Sandbox Code Playgroud)

这样可能吗,或者我只能通过新类型的隐式转换来重载运算符?(即,我必须显式创建1为该新类型的成员,并2对该特定类型使用隐式转换)。

scala operator-overloading

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

如何重载 [] 运算符以支持其他函数中的赋值和 const 参数?

考虑一个类SomeClass

class SomeClass{
    public:
        // Constructors, other members
        float& operator[](const unsigned i);
        friend bool operator==(const SomeClass &A, const SomeClass &B);
};
Run Code Online (Sandbox Code Playgroud)

假设这是==该类的运算符重载的方式(不是实际实现,而是过度简化的版本):

bool operator==(const SomeClass &A, const SomeClass &B){
    if (A[0] == B[0])
        return true;
    return false;
}
Run Code Online (Sandbox Code Playgroud)

这将引发编译器错误,因为重载[]运算符要求实例为 non- const。但是,如果我更改[]运算符的定义以允许const实例,则不能再进行赋值:

// ASSUMING: const float& operator[](const unsigned i) const;

SomeClass a;
a[0] = 0; // error, because the return value of [] is a reference to const!
Run Code Online (Sandbox Code Playgroud)

我真的不想const …

c++ oop class operator-overloading c++17

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

C++中赋值运算符的返回类型

想象我有一个这样的课程:

public:
A(const int a);
A& operator = (const A&);
};
Run Code Online (Sandbox Code Playgroud)

为什么“=”运算符的返回类型必须是“A&”而不是简单的“A”?

c++ operator-overloading assignment-operator

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

C++ 如何将 std::function 与不同返回类型的重载运算符一起使用

我正在尝试将标准std::function与自定义重载运算符一起使用。但是std::logical_and,在这种情况下Test,将string参数应用于我的班级是行不通的。

class Test {
public:
    std::string value;

    Test(std::string cvalue) : value(cvalue) {}

    std::string operator&& (const std::string& rhs) const {
        if (rhs == value) {
            return "true";
        }
        return "false";
    }
};

int main() {
    Test test("hey");
    std::string out = std::logical_and<std::string>()(test, "hey");
    std::cout << out << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

实现这一目标的正确方法是什么?我的预期输出是"true"

c++ operator-overloading

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

重载函数调用运算符 () 用于模板对象的索引和赋值

我正在尝试为矩阵数据结构创建模板,并且我希望以简洁直观的方式来索引和分配元素(即 'A(i,j)' 返回一个元素和 'A(i,j)=x ' 为该元素分配一个值。)

根据其他论坛主题,我看到通过引用返回数组元素,函数/运算符可以以这种方式返回和更改该元素。

template <typename T, int Rows, int Cols>
    struct Matrix {
        private:
        public:
            const int rows = Rows; //number of rows
            const int cols = Cols; //number of columns
            T data[Rows*Cols];     //contents of matrix

            //Single element indexing and assigment
            T& operator() (int i, int j) {
                return data[ (i-1)*(this->cols) + j ];
            }
    };

int main(){
    const int m = 3;
    const int n = 4;
    Matrix<float, m, n> A;

    for (int i = …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading

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

“???”是什么意思 在 Scala lang 中代表什么?

实际上,我有几个关于此代码片段的问题:

  1. 做 '???' 导致这个异常?
  2. 什么可以代替'???' ?
  3. '???' 是什么意思 代表?
object InfixTypesHard2 extends App {
  val x0: Int --- String --- Boolean = ???
}
    
class ---[T, V](t: T, v: V)
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

Exception in thread "main" scala.NotImplementedError: an implementation is missing
    at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
    at section3_OOP_operatorOverloading.InfixTypesHard2$.delayedEndpoint$section3_OOP_operatorOverloading$InfixTypesHard2$1(InfixTypesHard2.scala:5)
    at section3_OOP_operatorOverloading.InfixTypesHard2$delayedInit$body.apply(InfixTypesHard2.scala:3)
    at scala.Function0.apply$mcV$sp(Function0.scala:39)
    at scala.Function0.apply$mcV$sp$(Function0.scala:39)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
    at scala.App.$anonfun$main$1(App.scala:76)
    at scala.App.$anonfun$main$1$adapted(App.scala:76)
    at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:563)
    at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:561)
    at scala.collection.AbstractIterable.foreach(Iterable.scala:919)
    at scala.App.main(App.scala:76)
    at scala.App.main$(App.scala:74)
    at section3_OOP_operatorOverloading.InfixTypesHard2$.main(InfixTypesHard2.scala:3)
    at section3_OOP_operatorOverloading.InfixTypesHard2.main(InfixTypesHard2.scala)
Run Code Online (Sandbox Code Playgroud)

scala operator-overloading infix-operator

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

C++中new运算符的运算符重载

在以下代码中:

#include<stdlib.h>
#include<iostream>

using namespace std;

class Test {
public:
    void* operator new(size_t size);
    void operator delete(void*);
    Test() { cout<<"\n Constructor called"; }
    ~Test() { cout<<"\n Destructor called"; }
};

void* Test::operator new(size_t size)
{
    cout<<"\n new called";
    void *storage = malloc(size);
    return storage;
}

void Test::operator delete(void *p )
{
    cout<<"\n delete called";
    free(p);
}

int main()
{
    Test *m = new Test();
    delete m;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

new called
 Constructor called
 Destructor called
 delete called
Run Code Online (Sandbox Code Playgroud)

但是,当我调用 new …

c++ operator-overloading

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

为什么 new 和 delete 运算符签名与所有其他运算符不同

new 运算符的签名是:

void* operator new(size_t count)
Run Code Online (Sandbox Code Playgroud)

“operator”和“new”之间有一个空格。这是:

  1. 与所有其他运算符签名不同(除了 new、delete 及其对应的数组)。例如:

    T& operator=(const T& other)

  2. 不符合 C++ 不允许空格的函数名规则。

在这种情况下,我假设“operator”是一个关键字......但如果这是正确的,为什么在所有其他运算符函数签名中不是这种情况?

是否有不一致的解释?

c++ operator-overloading new-operator

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

尝试重载 &lt;&lt; 运算符时出错

在尝试重载 << 运算符时,我无法编译以下代码。任何人都可以指出出了什么问题吗?

#include <iostream>

std::ostream& operator<<(std::ostream& i, int n)
{
    return i;
}

int main()
{
    std::cout << 5 << std::endl;
    std::cin.get();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading stream c++11

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