标签: assignment-operator

构造函数或赋值运算符

你能帮助我吗C++标准中的定义描述了在这种情况下哪一个将被称为构造函数或赋值运算符:

#include <iostream>

using namespace std;

class CTest
{
public:

 CTest() : m_nTest(0)
 {
  cout << "Default constructor" << endl;
 }

 CTest(int a) : m_nTest(a)
 {
  cout << "Int constructor" << endl;
 }

 CTest(const CTest& obj)
 {
  m_nTest = obj.m_nTest;
  cout << "Copy constructor" << endl;
 }

 CTest& operator=(int rhs)
 {
  m_nTest = rhs;
  cout << "Assignment" << endl;
  return *this;
 }

protected:
 int m_nTest;
};

int _tmain(int argc, _TCHAR* argv[])
{
 CTest b = 5;

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

或者只是编译器优化的问题?

c++ copy-constructor assignment-operator

5
推荐指数
3
解决办法
3667
查看次数

c#赋值运算符&=

如果我有以下bool:

bool success = true;
Run Code Online (Sandbox Code Playgroud)

以下三行代码是否会成功存储相同的结果:

1 - success &= SomeFunctionReturningABool();

2 - success = success & SomeFunctionReturningABool();

3 - success = success && SomeFunctionReturningABool();
Run Code Online (Sandbox Code Playgroud)

我发现一篇文章说明1是2的快捷方式 - 但是2与3相同或者我的应用程序在执行此行时会爆炸...

c# assignment-operator

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

具有私有拷贝构造函数的类的C++ stl向量?

我们的代码中有一个类,比如class C.我想创建一个类对象的向量C.但是,有意声明复制构造函数和赋值运算符private.我不想(也许是不允许)改变它.

有没有其他干净的方式来使用/定义vector<C>

c++ private vector copy-constructor assignment-operator

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

在可移动和不可复制的类上使用移动和交换习惯是否有意义

如果我有一个类如

class Foo{
public:
    Foo(){...}
    Foo(Foo && rhs){...}
    operator=(Foo rhs){ swap(*this, rhs);}
    void swap(Foo &rhs);
private:
    Foo(const Foo&);
// snip: swap code
};
void swap(Foo& lhs, Foo& rhs);
Run Code Online (Sandbox Code Playgroud)

如果我没有复制构造函数,是否有意义实现operator = by value和swap?它应该防止复制我的类对象Foo但允许移动.

这个类是不可复制的,所以我不能复制构造或复制分配它.

编辑

我用这个测试了我的代码,它似乎有我想要的行为.

#include <utility>
#include <cstdlib>
using std::swap;
using std::move;
class Foo{
public: Foo():a(rand()),b(rand()) {}
        Foo(Foo && rhs):a(rhs.a), b(rhs.b){rhs.a=rhs.b=-1;}
        Foo& operator=(Foo rhs){swap(*this,rhs);return *this;}
        friend void swap(Foo& lhs, Foo& rhs){swap(lhs.a,rhs.a);swap(lhs.b,rhs.b);}
private:
    //My compiler doesn't yet implement deleted constructor
    Foo(const Foo&);
private:
    int a, b;
};

Foo …
Run Code Online (Sandbox Code Playgroud)

c++ rvalue-reference assignment-operator copy-and-swap c++11

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

重载赋值运算符而不返回语句

为什么允许赋值运算符返回void?为什么分配链在这种情况下起作用?看一下代码,我会非常清楚我在说什么.

码:

struct Foo
{
   std::string str;

   Foo(const std::string& _str)
   : str(_str)
   {
   }

   Foo& operator=(const Foo& _foo)
   {    
      str = _foo.str;
      //return *this; /* NO RETURN! */
   }
};

int main()
{
   Foo f1("1");
   Foo f2("2");
   Foo f3("3");
   f1 = f2 = f3 = Foo("4");

   std::cout << "f1: " << f1.str << std::endl;
   std::cout << "f2: " << f2.str << std::endl;
   std::cout << "f3: " << f3.str << std::endl;

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

问题:

  1. 为什么合法?(为什么要编译)
  2. 它为什么有效?

我读过很多地方"赋值运算符应返回*让你可以有任务链",这完全是有道理的,但后来为什么上面的工作?

试一试: …

c++ assignment-operator

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

堆栈上的构造函数/析构函数调用顺序

我有以下简单的代码:

class A
{
    int a;
public:
    A(int a) : a(a) { cout << "Constructor a=" << a << endl; }
    ~A()            { cout << "Destructor  a=" << a << endl; }
    void print()    { cout << "Print       a=" << a << endl; }
};

void f()
{
    A a(1);
    a.print();
    a = A(2);
    a.print();
}

int main()
{
    f();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

Constructor a=1
Print       a=1
Constructor a=2
Destructor  a=2
Print       a=2
Destructor  a=2
Run Code Online (Sandbox Code Playgroud)

我发现有两个析构函数调用with a=2和none,a=1 …

c++ constructor destructor assignment-operator

5
推荐指数
2
解决办法
684
查看次数

如何在Delphi中重载指定运算符以进行记录

我想制作使用动态数组的记录类型.

使用这种类型的变量A和B我希望能够执行操作A:= B(和其他)并且能够修改A的内容而无需修改B,如下面的剪切代码:

    type
      TMyRec = record
        Inner_Array: array of double;
      public
        procedure SetSize(n: integer);
        class operator Implicit(source: TMyRec): TMyRec;
      end;

    implementation

    procedure TMyRec.SetSize(n: integer);
    begin
      SetLength(Inner_Array, n);
    end;

    class operator TMyRec.Implicit(source: TMyRec): TMyRec;
    begin
    //here I want to copy data from source to destination (A to B in my simple example below)
    //but here is the compilator error
    //[DCC Error] : E2521 Operator 'Implicit' must take one 'TMyRec' type in parameter or result type
    end;


    var
      A, B: TMyRec; …
Run Code Online (Sandbox Code Playgroud)

delphi operator-overloading assignment-operator

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

在MATLAB中用两个值替换向量值

我要创建一个函数,需要输入一个向量v和三个标量a,bc.的函数替换的每一个元素v等于a具有两个元件阵列[b,c].

例如,给定v = [1,2,3,4]a = 2, b = 5, c = 5,输出将是:

out = [1,5,5,3,4]
Run Code Online (Sandbox Code Playgroud)

我的第一次尝试是试试这个:

v = [1,2,3,4];
v(2) = [5,5];
Run Code Online (Sandbox Code Playgroud)

但是,我得到一个错误,所以我不明白如何将两个值放在向量中的一个位置,即将所有以下值移到一个位置向右,以便新的两个值适合向量,因此,矢量的大小将增加一个.此外,如果有几个值a都存在于v,我不知道如何一次性全部更换.

我怎样才能在MATLAB中做到这一点?

arrays indexing matlab vector assignment-operator

5
推荐指数
2
解决办法
989
查看次数

Java赋值运算符行为与C++

这是在我处理"Cracking the Coding interview"问题时发生的:

编写一个函数来交换一个数字(即没有临时变量)

我决定用Java编写我的解决方案(因为我计划在实习面试中使用Java.)

我提出了一个解决方案,我几乎确信这是正确的答案(因为我在一行中做到了):

    public static void main(String args[]) {
        int a = 5;
        int b = 7;
        a = b - a + (b = a);
        System.out.println("a: " + a + " b: " + b);
    }
Run Code Online (Sandbox Code Playgroud)

当然,这段代码可以执行所需的结果.a == 7b == 5.

现在这是有趣的部分.

这段代码不会在C++中运行,也不是本书后面的解决方案.

所以我的问题是:为什么我的解决方案确实有效?我假设Java的做法与其他语言有所不同?

c++ java variable-assignment assignment-operator

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

为什么x - = x + 4返回-4而不是4

python的新手,并试图与赋值运算符的细节点搏斗.这是我的代码,然后是问题.

  x = 5
  print(x)
  x -= x + 4
  print(x)
Run Code Online (Sandbox Code Playgroud)

上面的代码,第一次返回5,但第二次打印时返回-4.在我脑海中,我觉得这个数字实际上应该是4,因为我正在读这个x = x - x +4.但是,我知道这是错误的,因为python正在返回-4.如果有人能向我解释(简单来说,因为我是新手),我会很亲切,因为我真的在这个桌子上敲了敲我的头.

python operators assignment-operator

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