标签: operator-keyword

是否可以在 C# 中创建自定义运算符?

可能的重复:
是否可以在 C# 中创建一个新的运算符?

我想知道是否有可能创建自定义运算符,例如if (a atleast 5)在 C# 中。

上述声明如下:if a is at least 5 then do this.

我正在寻找更多关键字类型运算符,例如typeofor is

c# operators keyword operator-keyword

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

赋值运算符中的内存泄漏

在我的程序中,我必须重载 = 运算符。重载函数如下所示:

Polygon &Polygon::operator=(const Polygon &source)
{
    this->capacity = source.capacity;
    this->index = source.index;
    this->vertex = new Vertex[source.capacity];

    for(int i = 0; i < source.capacity; i++)
    {
        this->vertex[i] = source.vertex[i];
    }

    return *this;
}
Run Code Online (Sandbox Code Playgroud)

但如果我学到了一件事,那就是我负责删除我用“new”关键字创建的东西。

所以在回来之前我尝试过:

delete vertex;
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为它删除了我刚刚复制到的对象。所以我尝试了:

delete source.vertex;
Run Code Online (Sandbox Code Playgroud)

这使我的程序在运行时崩溃了。

我也尝试过很多其他的方法,但它们只是有想法的尝试。我真的很希望你的帮助,不仅告诉我应该写什么,还告诉我在这些情况下应该如何思考。

c++ memory-leaks operator-keyword

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

Go:(运算符 + 未在切片上定义)

我正在尝试编写一个程序,该程序接受用户输入标志,读取包含数据的输入文件(testInput.txt),然后将用户的标志附加到输入数据并将其全部导出到输出文件(testOutput.txt)。当我尝试将两者附加在一起时,我在 func employeeReadWrite() 中遇到错误。“无效操作:内容+ cnvUserProfile(运算符+未在切片上定义)”。我是编程新手,go 是我的第一语言,而且我对切片还没有很好的掌握。我需要做什么来解决该错误?

package main

import (
"flag"
"fmt"
"io/ioutil"
"os"
)

var targetEmployee *string
var targetUsername *string
var targetLocation *string
var targetDepartment *string
var targetManager *string
var targetTitle *string
var userProfile string

func getFlagVariables() {
    targetEmployee = flag.String("employee", "", "What is there name? (-employee)")
    targetUsername = flag.String("username", "", "What is there username? (-username)")
    targetLocation = flag.String("location", "", "Where are the working from? (-location)")
    targetDepartment = flag.String("department", "", "What is there department? (-department)")
    targetManager = flag.String("manager", "", …
Run Code Online (Sandbox Code Playgroud)

append go slice operator-keyword

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

|变量在verilog中是什么意思?

我想知道assign hd_trs_detected = |hd_trs_match;verilog中的意思是什么。我最感兴趣的是这|hd_trs_match部分。我知道| 表示按位或,但不确定如何在 之前没有值的情况下解释它|。它是可理解的“1”还是“0”?|hd_trs_match如果它是 '0',那么使用与 just hd_trs_matchashd_trs_detected总是有任何优势相比,有什么优势hd_trs_match is?或者这本身就是一个明智的操作。

verilog bitwise-operators operator-keyword

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

将逗号运算符与条件运算符一起使用

,我正在学习 C++,并在使用and运算符时偶然发现了以下行为?:。条件运算符的语法如下所示E1 ? E2 : E3,其中 E1、E2 和 E3 是表达式[1],[2]。我从这段代码开始:

#include <iostream>

using namespace std;

int main(){
    int x = 20, y = 25;
    x > y ? cout << "x > y\n" , cout << "x is greater than y" : cout << "x !> y\n", cout << "x is not greater than y";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

和输出:

x !> y
x is not greater than y
Run Code Online (Sandbox Code Playgroud)

这就是我期待的结果。但是当我更改值以int x = 25, y = …

c++ comma-operator conditional-statements operator-keyword

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

在 C# 中,x+=y 和 x=x+y(x 和 y 都是简单类型)之间有什么性能差异?

在 C/C++ 中,

复合赋值运算符将简单赋值运算符与另一个二元运算符组合在一起。复合赋值运算符执行附加运算符指定的操作,然后将结果分配给左操作数。例如,复合赋值表达式,如

expression1 += expression2

可以理解为

expression1 = expression1 + expression2

然而,复合赋值表达式并不等同于扩展版本,因为复合赋值表达式只计算 expression1 一次,而扩展版本计算 expression1 两次:在加法运算和赋值运算中。

(引自Microsoft Docs


例如:

  1. 对于i+=2;,i将直接修改而不创建任何新对象。
  2. 对于i=i+2;i首先会创建一个副本。复制的一个将被修改,然后被分配回i.
        i_copied = i;
        i_copied += 2;
        i = i_copied;
Run Code Online (Sandbox Code Playgroud)

如果没有编译器的任何优化,第二种方法将构造一个无用的实例,从而降低性能。


在 C# 中,+=不允许重载类似运算符。并且所有像or一样的简单类型都声明为(这是否意味着 C# 中的所有结构实际上都是不可变的?)。intdoublereadonly struct

我不知道在C#,是有一定的表达力对象被修改(至少对于简单类型)直接,而不被创建的任何无用实例。

而且,是否有可能在C#-compiler优化表达x=x+yx+=y不如预期,如果有一个从构造函数和deconstructors无副作用。

c# operator-keyword

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

获取错误:尽管重载了 &lt;&lt; 运算符,但仍不匹配 'operator&lt;&lt;'(操作数类型为 'std::basic_ostream&lt;char&gt;' 和 'Complex')

我是一名新手程序员,我正在编写一个简单的程序,将两个复数相加。我<<以以下方式超载:

ostream& operator << (ostream& output, Complex &complex_num){

        output << complex_num.realPart << " + " << "(" << complex_num.imaginaryPart << ")i" <<endl;
        return output;

    }
Run Code Online (Sandbox Code Playgroud)

我的加法函数如下:

Complex operator +(Complex &c2){
        Complex temp;
        temp.realPart=realPart+c2.realPart;
        temp.imaginaryPart=imaginaryPart + c2.imaginaryPart;
        return temp;
    }
Run Code Online (Sandbox Code Playgroud)

在我的主函数中,当我尝试通过键入以下内容来打印结果时:

cout << "ADDITION OF THE TWO COMPLEX NUMBERS: "<<num1 + num2<< endl; 
Run Code Online (Sandbox Code Playgroud)

我收到一条错误消息,说与 operator 不匹配<<。但是,当我分配另一个对象num3 = num1 + num2然后编写以下代码时,程序运行良好。

cout << "ADDITION OF THE TWO COMPLEX NUMBERS: "<<num3<< endl; 
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?谁能帮帮我吗?

c++ overloading stream operator-keyword

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

有什么作用??生锈吗?

这里是它的一个例子动作

let msg = stream.next().await.context("expected a message")??;
Run Code Online (Sandbox Code Playgroud)

只是?做了两次吗?如果是这样,为什么在这种情况下需要这样做?

operator-keyword rust

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

C++ 重载 &gt;&gt; 运算符,根据 &gt;&gt; 的哪一侧进行不同的交互

我想知道是否可以通过这种方式重载 C++ >> 运算符

Account& operator>> (double left, Account &right)
{
    right.deposit(left);
    return right;
}

Account& operator>> (Account &left, double right)
{
    left.withdraw(right);
    return left;
}
Run Code Online (Sandbox Code Playgroud)

我想知道 >> 运算符是否具有此功能,所以我可以做这样的事情

account1 >> 200 >> account2
Run Code Online (Sandbox Code Playgroud)

这将从第一个账户中提取 200,然后存入第二个账户。

c++ overloading operator-keyword

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

我们如何在 C# 中解释 =&gt; Something =&gt; Something2 =&gt; Something3

我正在读 Enrico Buonanno 写的一本名为《C# 函数式编程》的书

public static Validator<T> FailFast<T>
   (IEnumerable<Validator<T>> validators)
   => t
   => validators.Aggregate(Valid(t), (acc, validator) => acc.Bind(_ => validator(t)));
Run Code Online (Sandbox Code Playgroud)

上面代码的原文是:

fail-fast 策略更容易实现:每个验证器都会返回一个 Validation,而 Validation 会暴露一个 Bind 函数,只有当状态为 Valid 时才应用绑定函数(就像 Option 和 Either),因此我们可以使用 Aggregate 来遍历列表验证器并将每个验证器绑定到运行结果。

FailFast 函数接受验证器列表并返回一个验证器:一个期望 T 类型的对象进行验证的函数。收到有效的 t 后,它使用 Valid(t) 作为累加器遍历验证器列表(如果验证器列表为空,则 t 有效),并使用 Bind 将列表中的每个验证器应用到累加器。

一共有三个 => 符号。这让我很难很好地理解代码。有谁熟悉 => 运算符并且可以用简单的英语解释它吗?多谢。

我还检查了文档中的 => 运算符。 https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-operator但文档中的演示并不像上面的代码那么复杂。

c# lambda function operator-keyword

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