标签: operator-keyword

Java - 'this' 运算符

我对 Java 中的 this 运算符有疑问。如果程序员会编写这样的代码:

private int counter;

public Incrementor(int counter){
this.counter = counter;
}
Run Code Online (Sandbox Code Playgroud)

避免阴影效果并使用这个会更好吗:

private int counter;

public Incrementor(int startValue){
counter = startValue;
}
Run Code Online (Sandbox Code Playgroud)

this 运算符不会在未来的编程执行中使 this 运算符过时吗?

java this operator-keyword

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

解决跨命名空间的模糊运算符

我创建了自己的<<重载bool因为我希望它打印truefalse不是10.这通常有效,除非我离开我定义此运算符的命名空间.在那种情况下,我得到一个模糊的操作错误.

error C2593: 'operator <<' is ambiguous
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(206): could be 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
with
[
    _Elem=char,
    _Traits=std::char_traits<char>
]
c:\Dummy.h(43): or       'std::ostream &Dummy::operator <<(std::ostream &,bool)'
while trying to match the argument list '(std::basic_ostream<_Elem,_Traits>, bool)'
with
[
    _Elem=char,
    _Traits=std::char_traits<char>
]
Run Code Online (Sandbox Code Playgroud)

如何指定从哪个命名空间中获取运算符?我天真地尝试过Dummy::<<,Dummy::operator<<但都没有奏效.

c++ namespaces operator-keyword

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

这个加法类会导致内存泄漏吗?

以下是可用于+操作的类的示例.

class A
{
public:
   int *array;

   A()
   {
      array = new int[10];
   }

   ~A()
   {
      delete[] array;
   }

   A operator+ (const A &b)
   {
      A c;
      for(int i=0; i<10; i++)
         c.array[i] += array[i] + b.array[i];
      return c;
   }
};

int main()
{
   A a,b,c,d;

   /* puts some random numbers into the arrays of b,c and d */
   a = b+c+d;
}
Run Code Online (Sandbox Code Playgroud)

a在复制结果之前是否会运行析构函数b+c+d?如果没有,我如何确保没有内存泄露?

+运算符重载设计这种方式使得没有操作数被修改.

c++ memory memory-leaks overloading operator-keyword

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

"错误:没有运算符"="匹配这些操作数"

我有这个奇怪的问题,编译器突出显示"="和"!="作为错误声称没有匹配的操作数,但我不知道如何.这是我的代码:

#pragma once
#include "Console.h"
#include "RandomNumber.h"
#include "Element.h"
#include "Flotsam.h"
#include "vector"
#include <list>
#include <iostream>

using namespace std;

#define NUMBER 10

int main()
{
    Console console;
    RandomNumber rnd;


    vector<Element*> flotsam;


    for(int i = 0; i < NUMBER; i++)
    {
        flotsam.push_back(new Element(rnd, console));
    }

    vector<Element>::iterator ptr;

    ptr = flotsam.begin();

    while(ptr!=flotsam.end())
    {
        ptr->printAt();
        ptr++; 
    }
    Sleep(1000);
    console.clear();

}
Run Code Online (Sandbox Code Playgroud)

c++ vector operator-keyword operand

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

复制构造函数调用析构函数c ++

我有一个my的测试类来制作我自己的字符串函数.我有复制析构函数的问题.

我有2个字符串:s1和s2.我调用函数s3 = s1 + s2;

它首先调用operator +函数,当它完成时调用析构函数.因此,operator = function中的字符串对象为空.我怎样才能解决这个问题?

析构函数:

String::~String() {
  if (this->str)
    delete[] str;
  str = NULL;
  len = 0;
}
Run Code Online (Sandbox Code Playgroud)

复制构造函数:

String::String(const String& string) {
  this->len = string.len;
  if(string.str) {
    this->str = new char[string.len+1];
    strcpy(this->str,string.str);
  } else {
    this->str = 0;
  }
}
Run Code Online (Sandbox Code Playgroud)

operator=:

String & String::operator= (const String& string) {
  if(this == & string)
    return *this;

  delete [] str;

  this->len = string.len;

  if(string.str) {
    this->str = new char[this->len];
    strcpy(this->str,string.str);
  } else {
    this->str = …
Run Code Online (Sandbox Code Playgroud)

c++ destructor copy-constructor operator-keyword

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

Java将ASCII值的字节数组转换为int

在将ByteBuffer读入b_array后,我试图将ascii值转换为int.

执行代码后我期望输出为129(b_array []具有ascii代码的十进制等值49,50,59)

有人可以告诉我,我在哪里做错了.我正在做一个0xFF,使其成为java中的无符号值,然后是OR运算来移动字节.

        byte[]  b_array=new byte[3];            
        buffer.get(b_array,0,3);            
        // Contents inside the b_array in ascii code
        //  b_array[0]=49;
        //  b_array[1]=50;
        //  b_array[2]=57;       

        int value=  b_array[2] & 0xFF | (b_array[1] & 0xFF) << 8 | (b_array[0] & 0xFF) << 16;       
        System.out.println(value);
Run Code Online (Sandbox Code Playgroud)

java byte bytearray operator-keyword

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

php中的运算符优先级

我们知道=优先级低于!.

我的问题:如果上述句子为真,那么如何执行以下if()条件

function foo()
{ 
   return false;
}


if(!$a=foo())
{
  echo "Yes, foo() appeared here.";
}
Run Code Online (Sandbox Code Playgroud)

php operator-precedence operator-keyword

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

haskell示例代码中的"<>"是什么意思?

我做了很多谷歌搜索,但没有弄清楚这个问题.部分原因是谷歌不将"<>"视为关键字.我在optparse-applicative样本的代码示例中看到了这个"运算符"

任何人都知道haskell中"<>"的含义请帮忙.谢谢!

haskell options operator-keyword

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

c#List.Exists返回true,返回"false && true"

所以我正在编写一个程序,对Listoperator的行为有点困惑.我有一个List,想知道它是否根据一些标准持有一个条目.现在让我们称它为List,obj具有以下属性:

public string PropA { get; set; }
public string PropB { get; set; }
public string PropC { get; set; }
public bool HasPropC { get; set; }
Run Code Online (Sandbox Code Playgroud)

现在我只想要一个真实的结果,如果PropA等于列表中的一个,如果列表中的属性具有PropC,它应该检查PropC,否则检查PropB.我用这段代码:

 if (List.Exists(x => {
       bool b = true;

       b = b && x.PropA.Equals(obj.PropA);
       b = b && x.HasPropC ? x.PropC.Equals(obj.PropC) : x.PropB.Equals(obj.PropB);

       return b;
}))
Run Code Online (Sandbox Code Playgroud)

在带有"PropA.Equals ..."的第一行之后,b被设置为false.但超越的界限再次成为现实.所以似乎有一个对象,即使它不是.我找到了一个解决方案,我将&& - Operator后面的第二行包装到括号中,但我仍然不知道为什么它是一个假的真正的.

你能给我一个暗示吗?

谢谢.

c# boolean list exists operator-keyword

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

这个表达式的含义是什么?

在 topcoder 上有一篇离散二分搜索文章,我在那里遇到了这个表达式。

while(lo & lt; hi ) if(i + j & lt ; = x )

如下面所描述的

我从未见过这个表达“;” 在 while 循环中并在其中使用 & 。

而且,一般来说,我想知道按AND &位运算符做什么(我知道它在二进制级别是如何工作的,但我想知道如何在不将数字转换为二进制形式的情况下计算最终结果)以及下面代码中的 if 语句.

int getMostWork(vector folders, int workers) {
  int n = folders.size();
  int lo = * max_element(folders.begin(), folders.end());
  int hi = accumulate(folders.begin(), folders.end(), 0);

  while (lo & lt; hi) {
    int x = lo + (hi - lo) / 2;

    int required = 1, current_load = 0;
    for (int i = 0; …
Run Code Online (Sandbox Code Playgroud)

c++ bit-manipulation operator-keyword

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