标签: operator-keyword

:: operator()做什么?

    struct reserved_memory
    {
     void *safety;
     size_t safety_size;
     reserved_memory(size_t size) : safety_size(size)
     {
       init();
     }

     bool use() {
        if (safety) {
            ::operator(safety); 
            safety=0;
            return true;
        } else
            return false;
     }
    private:
     void init() 
     {
        safety=::operator new(safety_size);
     }
   }
Run Code Online (Sandbox Code Playgroud)

我有这个没有编译的代码 - 我以前也没见过这个.这是调用构造函数吗?结构中没有重载()运算符...

c++ operator-keyword

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

运算符*未定义

I am trying to make a simple calculator that multiples. I have most of it done, but I keep getting the same error: The operator*is undefined for the argument types Double, EditText.

I searched the questions asked by others, but the only simple one dealt with something different than doubles. Does anyone know how to fix it?

package com.deitel.multiplicationtables;

import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View;

//Implements the listener for an onclick event (implements View.onClickListener) …
Run Code Online (Sandbox Code Playgroud)

java android undefined operator-keyword

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

是否可以在地图中使用运算符作为映射值?

我想做这样的事情:

int a = 9, b = 3;
map<char,operator> m;
m['+'] = +;
m['-'] = -;
m['*'] = *;
m['/'] = /;
for(map<char,operator>::iterator it = m.begin(); it != m.end(); ++it) {
    cout << func(a,b,it -> second) << endl;
}
Run Code Online (Sandbox Code Playgroud)

输出是这样的:

12
6
27
3
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

c++ map operator-keyword

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

c ++重载==运算符需要一个和两个参数

我正在尝试在我正在做的项目中重载==运算符.声明和定义是:

friend bool operator==(const TradeItem& item);

bool TradeItem::operator==(const TradeItem& item)
Run Code Online (Sandbox Code Playgroud)

当我编译它时,它说:'bool operator ==(const TradeItem&)'必须正好两个参数.所以我重做它有两个这样的论点:

friend bool operator==(const TradeItem& i1, TradeItem& i2);

bool TradeItem::operator==(const TradeItem& i1, TradeItem& i2)
Run Code Online (Sandbox Code Playgroud)

但是当我编译它时它告诉我它只需要一个参数....谈论给我一个解决方法.有谁知道什么出错了?

c++ overloading operator-keyword

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

当使用operator.itemgetter填充时,itertools.groupby返回空列表项

我正在尝试使用函数groupby和itemgetter,以便将元组的排序列表重新排列成组

from itertools import groupby
from operator import itemgetter

#initialize a list of tuples
indexed_qualityresults = [(u'moses-R4', 2.0), (u'moses-R4', 3.0), (u'lucy-R4', 3.0), (u'trados-R4', 2.0)]

#group tuples, using as a key the first element of each tuple
groupped_qualityresults = list(groupby(indexed_qualityresults, itemgetter(0)))

#print the key and the respective grouped tuples for each group
print "groupped_qualityresults =", [(a,list(b)) for a,b in groupped_qualityresults]
Run Code Online (Sandbox Code Playgroud)

输出是

groupped_qualityresults = [(u'moses-R4', []), (u'lucy-R4', []), (u'trados-R4', [(u'trados-R4', 2.0)])]
Run Code Online (Sandbox Code Playgroud)

如您所见,然后为tmy原始元组列表的两个第一个键返回的列表是空的,尽管它们不应该是.

预期产量:

groupped_qualityresults = [(u'moses-R4', [(u'moses-R4', 2.0), (u'moses-R4', 3.0)]), (u'lucy-R4', [(u'lucy-R4', …
Run Code Online (Sandbox Code Playgroud)

python python-itertools operator-keyword

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

使用"not equals"时,为什么if或operator不在if语句中工作?

我有点困惑.如果系统的当前用户名是!=Administrator或Administrator2,我想隐藏一个按钮,但实现我想要的目标的唯一方法是使用&&而不是||.

 string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

        if
            (userName != @"PC\Administrator" && userName != @"PC\Administrator2")
        {              
            button2.Hide();
        }
Run Code Online (Sandbox Code Playgroud)

但是,在我的形式打开的另一个地方button2,如果我使用==它然后似乎工作?

  string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
  var adminform = new AdminForm();

      if  (userName == @"PC\Administrator" || userName == @"PC\Administrator2")
        {
            adminform.Show();
        }
Run Code Online (Sandbox Code Playgroud)

知道为什么我在第一个例子中使用||时无法使用!=

c# equals operator-keyword

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

在MATLAB中为自定义类重载索引操作符`()`和`{}`

我希望能够在MATLAB中重载索引操作符 (){}我的类.特别是,我希望能够定义类方法,如...

% ...
classdef MyClass < handle

    % ... other class definition stuff...

    function returnVal = operator{}(indexRange)
        %....implementation here....
    end
    function returnVal = operator()(indexRange)
        %....implementation here....
    end
end
Run Code Online (Sandbox Code Playgroud)

这样我就可以创建对象并做...

x = MyClass();
returnedVals = x(:);
returnedCells = [x{:}];

% etc.
Run Code Online (Sandbox Code Playgroud)

这在MATLAB中是否可行?我知道这在C++和python中很容易(分别通过重载operator []__get__运算符).Mathworks网站本身并不太清楚如何做到这一点.

matlab operator-overloading operator-keyword

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

这个程序中"&"的区别是什么

以下是C++程序的一部分:

Circle circle1, &circle2 = circle1, *p = &circle2;
Run Code Online (Sandbox Code Playgroud)

我想知道两者之间&有什么区别?非常感谢.

c++ operator-keyword

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

如何访问通过引用operator = function传递的对象的私有数据?

我想知道我如何能够访问通过引用或值传递的对象的私有数据?这段代码有效.为什么?我需要一些解释.

class test_t {
    int data;
public:
    test_t(int val = 1): data(val){}
    test_t& operator=(const test_t &);
};

test_t& test_t::operator=(const test_t & o){
    this->data = o.data;
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

c++ overloading private operator-keyword

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

Java中*=运算符的运算符优先级是什么?

这来自Java Generics和Collections中的Sets部分.下面的示例用于说明如何计算String的哈希码:

    int hash = 0;
    String str = "The red fox jumped over the fence";
    /** calculate String Hashcode **/
    for ( char ch: str.toCharArray()){
//      hash *= 31 + ch; this evaluates to 0 ????
        hash = hash * 31 + ch;
    }
    p("hash for " + str + " is " + hash);
Run Code Online (Sandbox Code Playgroud)

哈利为"红狐狸跳过篱笆"是1153233987386247098.这似乎是正确的.但是,如果我使用简写符号,*=,我的答案为0.

    int hash = 0;
    String str = "The red fox jumped over the fence";
    /** calculate String Hashcode **/
    for ( char ch: …
Run Code Online (Sandbox Code Playgroud)

java operator-precedence operator-keyword

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