标签: operator-keyword

如何处理组合[] + =在Ruby中自动生成哈希?

为了实现Ruby哈希的自动生成,可以使用以下类

class AutoHash < Hash
  def initialize(*args)
    super()
    @update, @update_index = args[0][:update], args[0][:update_key] unless 
args.empty?
  end

  def [](k)
    if self.has_key?k
      super(k)
    else
      AutoHash.new(:update => self, :update_key => k)
    end
  end

  def []=(k, v)
    @update[@update_index] = self if @update and @update_index
    super
  end

  def few(n=0)
    Array.new(n) { AutoHash.new }
  end
end
Run Code Online (Sandbox Code Playgroud)

该类允许执行以下操作

a = AutoHash.new
a[:a][:b] = 1
p a[:c] # => {}             # key :c has not been created
p a     # => {:a=>{:b=>1}}  # note, that it does not …
Run Code Online (Sandbox Code Playgroud)

ruby hash autovivification operator-keyword

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

为什么operator =()函数不能由derive类继承

可能重复:
在C++中继承operator =的麻烦

我更新了代码

#include <QtCore/QCoreApplication>

class Base
{
    int num;
public:
    Base& operator=(int rhs)
    {
        this->num = rhs;
        return *this;
    }
};

class Derive : public Base
{
public:
    int deriveNum;
    using Base::operator =; // unhide the function
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Base base;
    Derive derive1, derive2;

    base = 1;  // calls Base::operator(1) and returns Base&

    derive1 = 11; // calls Base::operator(11) and returns Base&
    derive2 = 22; // calls Base::operator(22) and returns …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance operator-keyword

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

什么是":: operator new"和":: operator delete"?

我知道new并且delete是关键词.

int obj = new int;
delete obj;

int* arr = new int[1024];
delete[] arr;
Run Code Online (Sandbox Code Playgroud)

<new>header是C++标准头文件的一部分.它有两个运算符(我不确定它们是运算符还是函数):

::operator new

::operator delete

这些运算符使用如下:

#include <new>
using namespace std;

int* buff = (int*)::operator new(1024 * sizeof(int));
::operator delete(buff);
Run Code Online (Sandbox Code Playgroud)

什么是":: operator new"和":: operator delete"?它们newdelete关键字不同吗?

c++ new-operator delete-operator operator-keyword

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

用于否定除a =!a之外的布尔变量的任何快捷运算符

只是为了学习的缘故,我尝试了java doc,但我没有找到它.也许某些特殊技巧与任何运营商一样!!--

我喜欢自动增量 i++ = i=i+i

java boolean variable-assignment operator-keyword

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

赋值运算符重载Java

我无法弄清楚如何实现在C++中将赋值运算符重载到Java的等价物.我知道没有这样的东西,但我需要模拟它.我已经尝试重写Clone()函数,但没有运气.有任何想法吗?

以下是我的主要内容

 Queue p = new Queue();
 Queue q = new Queue();

    p.enqueue('a');
    p.enqueue(9);
    p.enqueue(10);
    p.enqueue(310);
    p.enqueue(8);

    q = p;
    System.out.print(p);
Run Code Online (Sandbox Code Playgroud)

这是克隆功能

public void Clone(Queue other) throws Throwable
{
    System.out.println("test\n");

    if(this == other)
    {

    }
    else
    {            
while(!isEmpty())
  dequeue();

Node tmp = other.head;
while(tmp != null){
    this.enqueue((T)tmp.element);
    tmp = tmp.next;

}   
    }

}
Run Code Online (Sandbox Code Playgroud)

java clone overloading variable-assignment operator-keyword

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

如果我想使用"a = {x,y};",那么要重载哪个运算符

我正在创建自己的课程,我希望能够像标题一样启动它.

a = {x, y};
Run Code Online (Sandbox Code Playgroud)

我无法找到任何指示我如何做到这一点.

c++ overloading operator-keyword

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

可以在派生类中运算符<<在c ++的基类中调用另一个运算符<<吗?

在我的代码中,Manager派生自Employee并且每个都有一个operator<<覆盖.

class Employee{
protected:
    int salary;
    int rank;
public:
    int getSalary()const{return salary;}
    int getRank()const{return rank;}
    Employee(int s, int r):salary(s), rank(r){};
};
ostream& operator<< (ostream& out, Employee& e){
    out << "Salary: " << e.getSalary() << " Rank: " << e.getRank() << endl;
    return out;
}

class Manager: public Employee{
public:
    Manager(int s, int r): Employee(s, r){};
};
ostream& operator<< (ostream& out, Manager& m){   
    out << "Manager: ";
    cout << (Employee)m << endl;  //can not compile, …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading derived-class operator-keyword

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

在c ++中重写= operator时如何初始化静态成员

请原谅我的英语.

我在我的班级中覆盖了operator =.现在我正在努力初始化一个静态成员.

我得到:错误:从'int'转换为非标量类型'TObj'请求

我的头文件:

#include <mutex>

template<typename T>
class TObj{
private:
    std::mutex m;
public:
    T val;
    // = coperation
     TObj& operator=(const T& rhs){
       m.lock();
       val = rhs;
       m.unlock();
       return *this;
    }

    operator T(){
        m.lock();     // THIS IS A BUG. Thank you Praetorian
        return val;   // RETURNS AND NEVER UNLOCKS
        m.unlock();   // DO NOT USE. Use lock_guard
    }

    ~TObj(){}

};


class OJThread
{
private:


public:
    OJThread();
    virtual void run() = 0;
    void start();
};
Run Code Online (Sandbox Code Playgroud)

我丑陋的cpp文件:

#include <iostream>
#include "ojthread.h"


using …
Run Code Online (Sandbox Code Playgroud)

c++ operator-keyword

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

C#运算符重载,重写字符串?

我对C#很新鲜当前正在学习运算符重载我正在尝试做这样的事情:

string val = 500; (I can't implicitly)
Run Code Online (Sandbox Code Playgroud)

然后

Number n1 = val;
Run Code Online (Sandbox Code Playgroud)

我设法得到了Number n1 = someintvalue,例如:

Number n1 = 500;
Run Code Online (Sandbox Code Playgroud)

像这样:

public struct Number
{
    public int Value { get; set; }
    public Number(int Val)
    {
        Value = Val;
    }
    public static implicit operator Number(int num)
    {
        return new Number(num);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当试图使数字n1 = val时; (当val是一个字符串时)我只是因为第一行无法编译而无法解决:

string val = 500;
Run Code Online (Sandbox Code Playgroud)

以下不会起作用:

public static implicit operator string(int A)
{
    return new string(A);
}
Run Code Online (Sandbox Code Playgroud)

因为1个错误我无法理解
1)用户定义的转换必须转换为封闭类型或从封闭类型转换

顺便说一句,我得到op过载的想法,
具体情况如下:return …

c# string overloading operator-overloading operator-keyword

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

Java +操作员行为未按预期继承

我正在学习用Java创建类,但是遇到了奇怪的行为。

我想创建一个名为的类My_class,该类具有的方法add。该方法应采用的成员My_class并将其添加到My_class括号中指定的的另一个成员中。

例如,运行此命令:

My_class first = new My_class(2);
My_class second = new My_class(6);
My_class answer = first.add(second);
System.out.println(answer);
Run Code Online (Sandbox Code Playgroud)

应打印:

8
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的代码My_class,但是我一直遇到My_class+操作员打交道的问题。我以为既然My_class是继承自int,那应该没有问题,但是显然我在这里缺少一些基本的东西。

public class My_class {
    /* Data members*/
    private int integer;

    /* Constructors */
    public My_class(int i) {integer = i;}

    /* Methods */
    public My_class add(My_class new_integer) {
        return integer + new_integer;
    }
Run Code Online (Sandbox Code Playgroud)

而不是预期的结果,我得到:

The operator + is undefined for the argument …
Run Code Online (Sandbox Code Playgroud)

java methods inheritance class operator-keyword

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