标签: assignment-operator

C++ - 重载默认类型的赋值运算符

我想为"int","long"等类型重载赋值运算符.也就是说,我想使用如下代码:

class CX {
private:
  int data;
...
};

CX obj;
int k;
k = obj;  // k should get the value of obj.data
Run Code Online (Sandbox Code Playgroud)

显然,赋值运算符不能成为友元函数.我如何实现上述目标?

我可能错过了一些简单的东西,但却无法弄清楚这样做的语法/方法.

另外,一个IMP限制 - 我们不能使用get/set方法因为::在发布代码中,我们将CX typedefed作为int或long(根据需要),但在DEBUG代码中,我们希望将它作为一个类(用于自动)在数千个地方进行类型检查).代码需要很常见.原因是如果CX成为一个类,编译器(至少我们使用的版本)在某种程度上无法优化所有操作.


一个问题是 - 我不想让它通过:

CX x; long p; p = x;
Run Code Online (Sandbox Code Playgroud)

我假设下面的转换解决方案将隐式地使长/短等代码也通过.(如果没有,那么它正是我要找的!).

在相关的说明中,回答David的问题 - 我想重构的原因是 - 我们希望能够将CX切换为32位或64位.因此,我们希望禁止任何隐式转换并在编译时捕获它们.现在,相反 - (disallowiong

CX x = some_64_bit_int;

但允许

CX x = some_32_bit_int;

我通过使用templatized =运算符来实现,该运算符默认情况下在编译时断言,但是将其重载为我想要的类型.


如果您认为这是一个糟糕的设计或我应该尝试其他替代方案 - 我需要的原因是:我们有数千行遗留代码,其中某些东西只是键入"int".

typedef int CX;
Run Code Online (Sandbox Code Playgroud)

整个地方都有作业,如:

CX x; int k; k = x;    // Writing the simplified relevant code here …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading assignment-operator

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

为什么我的for循环告诉我语法错误,插入"AssignmentOperator Expression"?

我有一些代码,如下:

int batchPosition = new Integer(batchBegin);

for (batchPosition;batchPosition<=batchEnd;batchPosition++)
Run Code Online (Sandbox Code Playgroud)

但我在eclipse中得到了这个错误:

Syntax error, insert "AssignmentOperator Expression" to complete ForInit.
Run Code Online (Sandbox Code Playgroud)

我已经看过关于这个错误的关于SO的各种帖子,并用谷歌搜索但我无法弄清楚为什么不允许这样做.

java for-loop assignment-operator

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

按值返回时不复制构造函数:C++

考虑一个类:

class loc{
    int x;
    int y;
    public:
    loc();
    loc(int x,int y);
    loc(const loc& l);//Copy Constructor
    loc operator + (const loc& l);
    loc operator - (const loc& l);
    loc& operator = (const loc& l);//Assignment Operator
    const loc& operator ++ ();
    friend ostream& operator << (ostream& os,const loc& l);
    friend istream& operator >> (istream& is,loc& l);
    ~loc();
    };
Run Code Online (Sandbox Code Playgroud)

当我调用Assignment运算符时:

int main()
{
loc Ob;


cout << "########\n\n";
cin >> Ob;
cout << "Ob : " << Ob;

cout << "\n\n########\n\n";

loc …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor assignment-operator

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

在JavaScript中使用赋值操作返回指定值的意图/想法是什么?

var foo, bar;
if (foo = true) {
    console.log('I am so true!');
}

if (bar = false) {
    console.log('Why am I not being logged?');
}
Run Code Online (Sandbox Code Playgroud)

同样在JavaScript控制台上,当我这样做时

var test;
test = 'I will be printed'
Run Code Online (Sandbox Code Playgroud)

字符串将被打印/返回.

在JavaScript中,赋值操作返回指定的值.这种行为是根据ECMAScript标准还是只是实施?这种行为与其他语言不同c,这有什么意图吗?如果有,那是什么?

javascript return-value assignment-operator

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

赋值操作总是从右到左复制数据?

看看这个Javascript代码:

var myString = new String();
myString.myObject = "myObject...";

//works fine as it shows "myObject..."
console.log("myString.myObject        :" + myString.myObject);
//OK myObject is also a string so it should give length and it works fine
console.log("myString.myObject.length :" + myString.myObject.length);
//it should not give anything or undefined since nothing is given to myString
console.log("myString                 :" + myString);
//myString is not yet defined so it should give undefined or 0..
console.log("myString.length          :" + myString.length);

//lets make a simple assignment
myString = myString.myObject;
//it …
Run Code Online (Sandbox Code Playgroud)

javascript assignment-operator

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

C++:在派生类构造函数中调用基类分配运算符的错误表单?

我知道对于独立类,你应该避免在复制构造函数中调用赋值运算符.复制和交换以及将重用代码移动到私有成员函数是两种轻松重用代码的方法.但是,最近我遇到了一些问题.这是代码:

// Derived.h
class Derived : Base {
  // bunch of fun stuff here
  // ...
  // constructor that builds a derived object from a base one
  explicit Derived(const Base& base);
  // Assignment operator for moving base class member variables into the derived one
  Derived& operator=(const Base& base);
};
// Derived.cpp
Derived::Derived(const& Base base) {
  *this = base; // use assignment operator from derived to base
}
Derived& Derived::operator=(const Base& base) {
  static_cast<Base>(*this) = base;  // call base class assignment …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance constructor assignment-operator

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


为什么Visual Studio不会进入我的赋值运算符?

我正在努力掌握移动和复制构造函数和作业.

我有以下简单的模板类:

template<typename T>
class Box {

public:
    // Copy constructor
    explicit Box(const T& val) { 
        value = val; 
    }

    // Move constructor
    explicit Box(const T&& val) {
        value = val;
    }

    // Set and get value
    void set(T val) { value = val; }
    T get() { return value; }

    // Copy assignment operator
    T& operator=(const T& val) {
        value = val;
    }

    // Move assignment operator
    T& operator=(const T&& val) {
        value = val;
    }

private:
    T value; …
Run Code Online (Sandbox Code Playgroud)

c++ assignment-operator

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

从赋值运算符调用析构函数会产生什么意外后果吗?

例如:

class Foo : public Bar
{
    ~Foo()
    {
        // Do some complicated stuff
    }

    Foo &operator=(const Foo &rhs)
    {
        if (&rhs != this)
        {
            ~Foo(); // Is this safe?

            // Do more stuff
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在继承和其他类似的事情上明确调用析构函数会产生什么意外后果吗?

有没有理由将析构函数代码抽象为void destruct()函数并调用它?

c++ destructor assignment-operator

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

or-assignment运算符(| =)如何工作?(C#)

我已经看到文档和答案(1) (2)试图解释| =运算符是什么以及它是如何工作的,虽然它在基本层面上有意义...我不太明白为什么或它如何实现它的功能.

解释说这a |= b相当于a = a | b,但我不知道如何评估是否给出a自身的值(a)或值b.根据我的理解,"或"意味着它可以是两件事之一,但没有具体说明它是两件事中的哪一件.

在Visual Studio中,我使用了一个名为Refactoring Essentials的扩展,它建议我用一个带有| =运算符的行替换我的一些代码,虽然代码在那里与运算符一起使用,但我对它如何完成它感到很遗憾,这促使我尝试在线研究它(因此,问这个问题).

我的代码来自

if (MessageBox.Show("Are you sure you want to cancel this operation?", "Confirm Cancel", MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No) == MessageBoxResult.No)
{
    e.Cancel = true;
}
Run Code Online (Sandbox Code Playgroud)

e.Cancel |= MessageBox.Show("Are you sure you want to cancel this operation?", "Confirm Cancel", MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No) == MessageBoxResult.No;
Run Code Online (Sandbox Code Playgroud)

它仍然有效.虽然我猜这e.Cancel是根据评估确定的MessageBox.Show(...) == MessageBoxResult.No,但我不知道为什么在那里需要| =运算符.为什么不使用标准赋值(=)运算符,因为表达式的结果是布尔值并 …

c# refactoring operators assignment-operator compound-assignment

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