标签: assignment-operator

为什么赋值运算符会执行与其匹配构造函数不同的操作?

我正在阅读一些提升代码,并且发现了这个:

inline sparse_vector &assign_temporary(sparse_vector &v) {
    swap(v);
    return *this;
}
template<class AE>
    inline sparse_vector &operator=(const sparse_vector<AE> &ae) {
        self_type temporary(ae);
        return assign_temporary(temporary);
    }
Run Code Online (Sandbox Code Playgroud)

它似乎将所有构造函数映射到赋值运算符.大.但是为什么C++会选择让他们做不同的事情呢?我能想到的只是scoped_ptr?

c++ constructor assignment-operator

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

分配变量时,"="和"=&"之间有什么区别?

我想弄清楚$a=&$b和之间的区别$a=$b.我知道&将变量作为参考变量.但是下面的测试给了我相同的结果.有人可以解释这个区别吗?谢谢.

 $a=5;
 $b=6;

 $a=&$b;
 echo $a; //6


 $a=5;
 $b=6;

 $a=$b;
 echo $a; //6
Run Code Online (Sandbox Code Playgroud)

php variables operators assignment-operator

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

可空类型和赋值运算符

我一直认为.NET框架的可空类型只是框架给我们的一个很好的构造,但并没有添加到语言本身的任何新东西.

也就是说,直到今天,为了演示,我尝试创建自己的Nullable结构.

我得到了我需要的一切,除了这样做的能力:

var myInt = new Nullable<int>(1);
myInt = 1;
Run Code Online (Sandbox Code Playgroud)

问题是第二个语句,因为我不能以任何方式重载赋值运算符.

我的问题是:这是一个特殊情况,在赋值运算符有过载的语言中?如果没有,你会如何使之前的例子有效?

谢谢!

.net c# nullable operator-overloading assignment-operator

4
推荐指数
2
解决办法
844
查看次数

在C#中使用| =运算符时会发生什么?

如果我使用这样的东西:

xr.Settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
Run Code Online (Sandbox Code Playgroud)

究竟是什么|=完成的?

.net c# operators assignment-operator

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

带下标运算符的重载赋值运算符

我重载了下标运算符和赋值运算符,我试图Array x; x[0]=5; 通过重载下标运算符来获得赋值运算符示例的正确值, 我可以获得值 0,但是当我重载赋值运算符时,它会进行赋值,但它不使用我的重载函数,因为可用2 的值应该是 5。

class Array
{

public:
    int *ptr;
    int one,two;
    Array(int arr[])
    {
        ptr=arr;
    }

    int &operator[](int index)
    {
        one=index;
        return ptr[index];
    }
    int & operator=(int x){
        two=x;
        return x;
    }   
};

int main(void)
{
    int y[]={1,2,3,4};
    Array x(y);
    x[1]=5;
    cout<<x[0]<<endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-overloading subscript assignment-operator

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

什么时候应该让课程不可复制?

根据谷歌风格指南,"很少有类需要复制.大多数应该既没有复制构造函数也没有赋值运算符."

他们建议您使类不可复制(即,不给它复制构造函数或赋值运算符),而是建议在大多数情况下通过引用或指针传递,或使用无法隐式调用的clone()方法.

但是,我听到了一些反对这一点的论点:

  • 访问引用(通常)比访问值慢.
  • 在某些计算中,我可能希望保留原始对象,只返回已更改的对象.
  • 我可能想将计算的值存储为函数中的本地对象并返回它,如果我通过引用返回它,我就无法做到.
  • 如果一个类足够小,则通过引用传递较慢.

遵循本指南的正面/负面是什么?是否有任何标准的"经验法则"使类不可复制?创建新类时我应该考虑什么?

c++ class copy-constructor assignment-operator

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

为什么从未调用我的复制赋值运算符?

我正在玩c ++ 14,我想知道为什么我的赋值运算符从未被调用过.实现似乎是正确的,我禁用了优化(-fno-elide-constructors -O0)这是某种编译器优化我缺少或我的代码有问题?

源代码

#include <iostream>

using namespace std;

int num = 0;

#define LOG_LINE(a) cout << "\n" << (++num) << ".)------------------------> " << #a << "\n"
#define LOG_TEXT cout << "called " << __PRETTY_FUNCTION__ << "\n"

struct Klass {
    Klass() { LOG_TEXT; }
    ~Klass() { LOG_TEXT; }

    // copy
    Klass(const Klass&) { LOG_TEXT; }
    Klass& operator=(const Klass&) { LOG_TEXT; return *this; }

    // move
    Klass(Klass&&) { LOG_TEXT; }
    Klass& operator=(Klass&&) { LOG_TEXT; return *this; }
}; …
Run Code Online (Sandbox Code Playgroud)

c++ assignment-operator c++14

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

在副本和交换习语中实现交换

下面什么是复制和交换习惯用法以及如何为我的班级提供交换功能,我尝试实现交换功能,如后面接受的答案选项2(具有调用成员函数的自由函数)而不是直接友好前链接中的自由功能.

但是以下不编译

#include <iostream>

// Uncommenting the following two lines won't change the state of affairs
// class Bar;
// void swap(Bar &, Bar &);
class Bar {
public:
  Bar(unsigned int bottles=0) : bottles(bottles) { enforce(); } // (1)
  Bar(Bar const & b) : bottles(b.bottles) { enforce(); } // (1)

  Bar & operator=(Bar const & b) {
    // bottles = b.bottles;
    // enforce();
    // Copy and swap idiom (maybe overkill in this example)
    Bar tmp(b); // …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor assignment-operator copy-and-swap c++11

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

在未实现赋值运算符时调用构造函数

我在Visual Studio 2015中练习'String'类实现(C++).我的类中有3个构造函数,而不是任何赋值运算符.

String();
String(char _c);
String(const char* _pc);
Run Code Online (Sandbox Code Playgroud)

main(),我故意使用赋值运算符来检查代码的行为.

令我惊讶的是,它没有给出任何错误,并使用构造函数String(const char* _pc)为对象赋值.此外,在范围的最后,它调用析构函数两次.

在这种情况下,编辑器在幕后做什么?为什么?

这是我的代码:

class String {
    private:
        int capacity;
        char* start;

    public:
        //Constructors
        String();
        String(const char* _pc);

        //Destructor
        ~String();
}

String::String() :start(nullptr), capacity(0) {}

String::String(const char* _pc) :capacity(1) {
    const char* buffer = _pc;

    while (*(buffer++))
        capacity++;

    int temp_capacity = capacity;
    if (temp_capacity)
        start = new char[temp_capacity];

    while (temp_capacity--) {
        start[temp_capacity] = *(--buffer);
    }
}

String::~String() {
    if (capacity == 1)
        delete start;
    if …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor assignment-operator

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

标量 vs 列表赋值运算符

请帮助我理解以下片段:

  • my $count = @array;
  • my @copy = @array;
  • my ($first) = @array;
  • (my $copy = $str) =~ s/\\/\\\\/g;
  • my ($x) = f() or die;
  • my $count = () = f();
  • print($x = $y);
  • print(@x = @y);

perl assignment-operator

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