标签: operator-keyword

可交换的oprator重载+ 2个不同的对象

我有2个表示矩阵的类:
1.RegularMatrix - O(n ^ 2)表示
2. SparseMatrix - 表示为链表(没有零)的矩阵.

让我说我有:

RegularMatrix a;
SparseMatrix b;
Run Code Online (Sandbox Code Playgroud)

我希望能够做到:

a+b;
Run Code Online (Sandbox Code Playgroud)

并且:

b+a;
Run Code Online (Sandbox Code Playgroud)

所以我正在重载+运算符.我的问题是,因为我希望加法是可交换的(a + b = b + a),我是否需要实现2个过载,每种情况一个?

RegularMatrix operator+(const RegualarMatrix &, const SparseMatrix &);
RegularMatrix operator+(const SparseMatrix & ,const RegualarMatrix &);
Run Code Online (Sandbox Code Playgroud)

或者是否有编译器自己决定的一般形式?

谢谢

c++ operator-overloading operator-keyword commutativity

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

operator <<重载ostream

为了使用cout:std :: cout << myObject,为什么我必须传递一个ostream对象?我认为这是一个隐含的参数.

ostream &operator<<(ostream &out, const myClass &o) {

    out << o.fname << " " << o.lname;
    return out;
}
Run Code Online (Sandbox Code Playgroud)

谢谢

c++ overloading ostream operator-keyword

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

语法与算子相关性之间的关系

一些编译器书籍/文章/论文谈论语法的设计及其运算符的关联性的关系.我是自上而下的狂热粉丝,特别是递归下降,解析器和迄今为止我编写的大多数(如果不是全部)编译器都使用以下表达式语法:

Expr   ::= Term { ( "+" | "-" ) Term }
Term   ::= Factor { ( "*" | "/" ) Factor }
Factor ::= INTEGER | "(" Expr ")"
Run Code Online (Sandbox Code Playgroud)

这是该BNF的EBNF表示:

Expr  ::= Term Expr'
Expr' ::= ( "+" | "-" ) Term Expr' | ?
Term  ::= Factor Term'
Term' ::= ( "*" | "/" ) Factor Term' | ?
Factor = INTEGER | "(" Expr ")"
Run Code Online (Sandbox Code Playgroud)

根据我所读到的,有些人认为这种语法是"错误的",因为操作员关联性的变化(这4个操作员从左到右)由不断增长的解析树向右而不是向左证明.对于通过属性语法实现的解析器,这可能是正确的,因为l-attribute值要求首先创建此值,然后传递给子节点.然而,当使用普通的递归下降解析器实现时,由我决定是先构造此节点然后传递给子节点(自上而下)还是先创建子节点然后将返回的值添加为此节点的子节点(通过在这个节点的构造函数中)(自下而上).我应该在这里找到一些东西,因为我不同意这句话说这个语法是"错误的",而且这种语法已被用于许多语言中.Wirthian的.通常(或全部?)表示它的读数会促进LR解析而不是LL.

parsing associativity operator-keyword

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

Laravel 多个 WHERE() 运算符优先级

我使用 Eloquent 编写了以下查询:

Contact::select(DB::raw("DATE_FORMAT(DATE(`created_at`),'%b %d') as date"))
                                 ->addSelect(DB::raw("`created_at`"))
                                 ->addSelect(DB::raw("COUNT(*) as `count`"))
                                 ->where('created_at', '>', $date)
                                 ->ofType($type)
                                 ->groupBy('date')
                                 ->orderBy('created_at', 'ASC')
                                 ->lists('count', 'date');
Run Code Online (Sandbox Code Playgroud)

你可以看到它使用了一个查询范围方法ofType()这是那个方法,它只是向查询添加了一堆额外的 where 子句:

return $query->where('list_name', '=', 'Apples')
            ->orWhere('list_name', '=', 'Oranges')
            ->orWhere('list_name', '=', 'Pears')
            ->orWhere('list_name', '=', 'Plums')
            ->orWhere('list_name', '=', 'Blueberries');
Run Code Online (Sandbox Code Playgroud)

最终这会产生以下真实的 SQL 查询:

SELECT DATE_FORMAT(DATE(`created_at`),'%b %d') as date,`created_at`, COUNT(*) as `count` 
FROM `contacts` 
WHERE `created_at` > '2014-10-02 00:00:00' 
AND `list_name` = 'Apples' 
OR `list_name` = 'Oranges' 
OR `list_name` = 'Pears' 
OR `list_name` = 'Plums' 
OR `list_name` = 'Blueberries' 
GROUP …
Run Code Online (Sandbox Code Playgroud)

mysql operator-precedence operator-keyword laravel eloquent

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

如何控制operator []赋值

我知道如何重载operator[]如下:

T& operator [](int idx) {
    return TheArray[idx];
}

T operator [](int idx) const {
    return TheArray[idx];
}
Run Code Online (Sandbox Code Playgroud)

但我想要的是控制分配的值arr[i] = value.我想将值控制在0到9之间.有没有语法可以这样做?

c++ operator-overloading operator-keyword

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

`operator()...`在C++代码中意味着什么?

我试图理解std::visit来自cppreference的例子,我在哪里看到以下代码行:

template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
Run Code Online (Sandbox Code Playgroud)

我不明白.operator()...代码中的含义是什么?

c++ templates variant operator-keyword c++17

6
推荐指数
3
解决办法
1277
查看次数

在没有返回值优化的情况下将两个对象添加到一起时会创建多少个临时对象?

在阅读Scott Meyers所着的"更有效的C++"一书的第20和22项后,我决定提出这个问题.

假设你写了一个代表有理数的类:

class Rational
{
public:
    Rational(int numerator = 0, int denominator = 1);

    int numerator() const;
    int denominator() const;

    Rational& operator+=(const Rational& rhs); // Does not create any temporary objects
    ...
};
Run Code Online (Sandbox Code Playgroud)

现在让我们说你决定operator+使用operator+=:

const Rational operator+(const Rational& lhs, const Rational& rhs)
{
    return Rational(lhs) += rhs;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:如果禁用返回值优化,将创建多少个临时变量operator+

Rational result, a, b;
...
result = a + b;
Run Code Online (Sandbox Code Playgroud)

我相信会创建2个临时值:一个Rational(lhs)是在body体内执行的operator+,另一个operator+是在返回的值是通过复制第一个临时值创建的.

当Scott提出这个操作时,我的困惑出现了:

Rational result, a, b, …
Run Code Online (Sandbox Code Playgroud)

c++ effective-c++ operator-keyword return-value-optimization copy-elision

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

加号运算符的 C++ 重载

我想通过重载 + 运算符来添加 2 个对象,但是我的编译器说没有匹配的函数可以调用 point::point(int, int)。有人可以帮我处理这段代码,并解释错误吗?谢谢你

#include <iostream>

using namespace std;

class point{
int x,y;
public:
  point operator+ (point & first, point & second)
    {
        return point (first.x + second.x,first.y + second.y);
    }
};

int main()
{
    point lf (1,3)
    point ls (4,5)
    point el = lf + ls;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ int sum operator-overloading operator-keyword

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

如何在 Swift 中找到运算符定义?

我写代码如下:

let array1: [Int] = [0,1]
let array2 = array1 + [2]
Run Code Online (Sandbox Code Playgroud)

它只是有效。我想找到+操作符定义的位置。我在工作区中搜索 ArrayExtension 没有结果。我搜索 Apple doc Collection Sequence Array,没有结果。

有没有办法导航到operator definitionCMD + CTRL + J比如func

xcode definition operator-keyword swift

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

(??=) 双问号和等号,那个运算符有什么作用?

一旦我偶然发现带有 operator 的 php7 代码??=。我试图搜索,它清楚地做了什么,但无法轻松找到。我试图读出 php 操作符,甚至大多数官方资源都有所有操作符的描述,甚至像.=,这样的复合操作符+=,但没有描述??=

例如,PHP Operators保留了所有运算符的描述,如直接形式 ( ., +),复合形式 ( .=, +=),但没有??=,因此我首先感到困惑并认为这完全是另一回事。问题简单明了,但整个案例有点混乱,这就是为什么我试图帮助像我这样的其他 php 初学者

operator-keyword php-7.4

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