标签: assignment-operator

当 T 包含一个 `const` 数据成员时,为什么会删除 `std::optional<T>::operator=`?

以下代码会导致编译器错误:

#include <optional>

class A {
};
class B {
private:
    const A a;
};

int main()
{
    B b;
    std::optional<B> bo1;
    bo1 = b;
}
Run Code Online (Sandbox Code Playgroud)

例如,在 gcc 上,错误显示为:

#include <optional>

class A {
};
class B {
private:
    const A a;
};

int main()
{
    B b;
    std::optional<B> bo1;
    bo1 = b;
}
Run Code Online (Sandbox Code Playgroud)

在 MSVC 上,作为另一个例子,错误显示:

main.cpp: In function 'int main()':
main.cpp:12:7: error: uninitialized const member in 'class B'
12 |     B b;
   |       ^
main.cpp:7:13: note: 'const A …
Run Code Online (Sandbox Code Playgroud)

c++ assignment-operator c++17 stdoptional

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

标准库类上的自定义赋值运算符

例如,如果我想进行作业

std::vector<int> a;
std::vector<std::pair<std::string, int>> b;
a = b;  // a takes all the second components of the pairs in b
Run Code Online (Sandbox Code Playgroud)

这样的自定义分配可能吗?

c++ assignment-operator

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

构造函数与“=”运算符执行相同的操作吗?

  1. 当我们没有定义任何=运算符时,编译器如何知道使用构造函数?
  2. 构造函数不是只在定义变量时调用吗?
#include <string>

class Person
{
public:
    std::string name;
    Person(const char* fullName) : name(fullName) {}
};

int main()
{
    Person p1("Jibel Sadeghi"); // This is ok because we declared the constructor with a const char*
    /* When we didn't overload the '=' operator for 'const char*',
       how the next lines don't have any errors? */
    p1 = "Ben Sadeghi";
}
Run Code Online (Sandbox Code Playgroud)

c++ oop constructor operator-overloading assignment-operator

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

C中的作业

你能用C进行多次赋值操作吗?

int a = 0, b = 0, c = 0, d = 0;
(((a = b) = c) = d);
Run Code Online (Sandbox Code Playgroud)

我已经读过某个地方,C标准声明这个结果不会是左值?这是不确定的?

c assignment-operator

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

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

为什么不能保证赋值的成本低于构造函数析构函数对?

在Scott Meyers的"Effective C++"中第26项:尽可能推迟变量定义,赋值的成本肯定不会低于构造函数 - 析构函数对.

然后,对于不同的数据类型或类,如何将赋值的成本与构造函数 - 析构函数对进行比较?哪一个会更便宜?为什么?

在我看来,构造函数 - 析构函数对需要分配和释放内存,而构造函数至少会初始化变量.但是赋值只需要改变变量的值.

所以,我认为一般来说,赋值的成本应该小于构造函数 - 析构函数对.

c++ constructor destructor assignment-operator

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


C = C++; 为什么该值不会改变C变量,因为它保持为0

当我使用c=c++;指令时,为什么C的值不会改变.

#include <stdio.h>
int main()
{
int t, c=0,d;
scanf("%d",&t);
while(t--)
{
    int n;
    scanf("%d",&n);
    if(n>=50)
    {
        c=c++;
        printf("%d\n",c);
    }
}

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

c assignment-operator post-increment

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

Go 中哪些语义规则决定何时发生单值赋值与何时发生双值赋值?

在研究map围棋之旅:不同诱变地图,有一件事我发现令人意外的是,我们可以使用任一值分配或两个值分配访问值的关键在一个地图。示例代码:

package main

import (
    "fmt"
)

func main() {
    m := map[int]int{2: 4, 3: 9, 4: 16}
    
    // Example 1
    fmt.Println(m[2])
    
    // Example 2
    v := m[2]
    fmt.Println(v)
    
    // Example 3
    v, ok := m[2]
    fmt.Println(v, ok)
}
Run Code Online (Sandbox Code Playgroud)

输出:

4
4
4 true
Run Code Online (Sandbox Code Playgroud)

使用相同的语法支持一值和二值赋值涉及哪些语义规则?Go 中是否还有其他这样的特殊形式,根据赋值运算符的左侧,以相同的语法支持一值和二值赋值?

此外,我可以自己编写一个函数foo(),根据赋值运算符的左侧返回一个值或两个值吗?

syntax dictionary go assignment-operator semantics

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

在单链接列表中重载赋值运算符

我正在学习链表.我创建了一个模板实现,包括构造函数,插入器,析构函数,复制构造函数和重载赋值运算符.问题是我的测试程序在重载赋值运算符后没有输出任何内容.

对于我的赋值运算符,我使用Clear()函数在复制之前完全清除列表.我把它放在析构函数中并检查它是否正常工作.我还检查了我的复制构造函数,它也运行良好.

文件node.h:定义节点构建块

#include <iostream>
using namespace std;

template <typename T>
struct Node{
    T _item;
    Node<T>* _next;

    Node() {
        _item = T();
        _next = NULL;
    }

    Node(T item){
        _item = item;
        _next = NULL;
    }

    // Print the value of a node
    friend std::ostream& operator <<(std::ostream& outs, const Node<T> &printMe){
        outs << "[" << printMe._item << "]";
        return outs;
    }
};
Run Code Online (Sandbox Code Playgroud)

文件list.h:定义链接列表模板

#include "node.h"

template <class T>
class List {
public:

    // …
Run Code Online (Sandbox Code Playgroud)

c++ linked-list operator-overloading assignment-operator

-2
推荐指数
1
解决办法
120
查看次数