标签: assignment-operator

C++ 错误:只读变量的赋值

我有一些返回此错误的 C++ 代码:

\n\n
error: assignment of read-only variable \xe2\x80\x98parking\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码:

\n\n
char const * const parking= "false";\n\nif (phidgets.value(PHIDGET3V_1) > 1000) {\n    parking = "true";\n} \nelse{\n    parking = "false";\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

此错误是什么意思以及如何修复它?

\n

c++ constants assignment-operator

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

标准库类型的赋值运算符的 ref 限定符

我想知道,标准类型的赋值运算符没有左值引用限定是否有原因?他们都不是。

因此,我们可以这样写:

std::string{} = "42";
std::string s = "hello " + std::string{"world"} = "oops!";

std::vector<int> v = { 1,2,3 };
std::move(v) = { 4,5,6 };
Run Code Online (Sandbox Code Playgroud)

如果赋值运算符是左值引用限定的,则所有这些示例都将无法编译。

是因为有很多东西需要修改(但后来是为了 noexcept)而没有人写提案?我不认为人们会写这样的代码,但库不应该被设计成甚至不允许这样做吗?

c++ stl std assignment-operator ref-qualifier

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

可以采取哪些措施来防止对返回值进行误导性分配?

使用 C++ 多年后,我意识到使用自定义类时语法中的一个怪癖。尽管是正确的语言行为,但它允许创建非常具有误导性的界面。

这里的例子:

class complex_arg {
    double r_;
    double phi_;
 public:
    std::complex<double> value() const {return r_*exp(phi_*std::complex<double>{0, 1});}
};

int main() {
    complex_arg ca;
    ca.value() = std::complex<double>(1000., 0.);  // accepted by the compiler !?
    assert( ca.value() != std::complex<double>(1000., 0.) ); // what !?
}
Run Code Online (Sandbox Code Playgroud)

https://godbolt.org/z/Y5Pcjsc8d

可以对类定义做什么来防止这种行为? (或者至少标记类的用户第三行并没有真正执行任何分配。)

我只看到一种出路,但它需要修改类,并且它不能很好地扩展(对于可以移动的大型类)。

    const std::complex<double> value() const;
Run Code Online (Sandbox Code Playgroud)

我也尝试过[[nodiscard]] value(),但没有帮助。

作为最后的手段,也许可以对返回的类型做一些事情std::complex<double>(也就是说,假设一个人控制着该班级)


请注意,我知道有时可能需要(优化)分配给新获得的值并将其传递给另一个函数f( ca.value() = bla )。我并不是质疑这种用法本身(尽管它也很令人困惑);我的问题主要是ca.value() = bla;作为一个独立的声明,它并不像它看起来的那样。

c++ rvalue user-defined-types assignment-operator

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

为什么编译器找不到赋值运算符?

template <typename T>
class MyPointer
{public:


    template <typename U>
    void operator=(MyPointer<U>&& other)
    {
      
    }
    char* get() const { return pointer; }
private:
    char* pointer;
};

int main()
{
    struct B {};
    struct D : B{};   

    MyPointer<B> my_pointer_b;
    MyPointer<D> my_pointer_d;

    my_pointer_b = my_pointer_d;


}
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

二进制“=”:找不到采用“MyPointermain::D”类型的右侧操作数的运算符(或者没有可接受的转换)

编译器为我使用的特定类型实例化赋值运算符,因此即使它删除了默认运算符,实例化的运算符也应该在那里。

c++ assignment-operator

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

为什么将容器的元素分配给容器(而不是)定义良好的 C++?

在 C++ 中,存在臭名昭著的自赋值问题:在实现 时operator=(const T &other),必须小心,this == &other不要this在从 复制数据之前破坏 的数据other

然而,*thisother可能以比作为同一个对象更有趣的方式交互。即,一个可以包含另一个。考虑以下代码:

#include <iostream>
#include <string>
#include <utility>
#include <vector>
struct Foo {
    std::string s = "hello world very long string";
    std::vector<Foo> children;
};
int main() {
    std::vector<Foo> f(4);
    f[0].children.resize(2);
    f = f[0].children;  // (1)
    // auto tmp = f[0].children; f = std::move(tmp);  // (2)
    std::cout << f.size() << "\n";
}
Run Code Online (Sandbox Code Playgroud)

我希望行(1)(2)是相同的:程序被明确定义为 print 2。然而,我还没有找到一个编译器+标准库组合,可以与启用的行 …

c++ assignment-operator language-lawyer copy-assignment memory-aliasing

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

C#中的方法参数赋值

给定下面的代码,bar(int y)中的方法参数y是否会被赋予x或1中的值?我意识到它们在逻辑上是等价的,但我想了解赋值操作.

class Program
{
    static void Main(string[] args)
    {
        var foo = new Foo();
        var x = 0;
        foo.Bar(x = 1);
    }
}

public class Foo
{
    public void Bar(int y)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

c# assignment-operator

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

php:$ a = $ b OR $ a = $ c vs.三元

我需要将第二个变量的值分配给第三个变量,如果第一个变量是(bool)false或未定义,则需要使用第二个变量的值.

我通常使用三元表示法这样做:

$foobar = ($some_prefix_and_some_variable_name) ? $some_prefix_and_some_variable_name : $bar ; 
Run Code Online (Sandbox Code Playgroud)

但是,如果$ foo变量名称很长,有时这不是很漂亮,因为它需要在这种表示法中重复.

我现在的问题是,使用这种表示法是否同样好:

$foobar = $some_prefix_and_some_variable_name OR $foobar = $bar;
Run Code Online (Sandbox Code Playgroud)

这个符号可以与三元版本互换吗?

php boolean-logic ternary-operator variable-assignment assignment-operator

3
推荐指数
2
解决办法
1230
查看次数

如何创建可以初始化C++数据类型的类?

标题基本上都说明了一切.我主要想这样做,这样我就可以创建一个对象(比如一个自定义字符串对象),可以初始化其他API中其他函数的参数.以下是我尝试使用自定义整数类的示例:

#include <iostream>
using namespace std;

class test
{
public:
    int member;
    test(int i) : member(i) {}

    friend int &operator=(int &i, test t);
};

int &operator=(int &i, test t)
{
   return (i = t.member);
}

int main()
{
    int i;
    test t = 90;

    cout << (i = t);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是我收到一个错误,说operator =需要是一个成员函数.我理解C++标准的目标是防止赋值运算符的静态和非成员重载被实现,但有没有其他方法可以做到这一点?感谢您的任何帮助/建议!

c++ overloading friend assignment-operator operator-keyword

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

为什么opAssign不能为类重载?

该表说,赋值重载只适用于结构,而不是类.这让我感到惊讶.不是A = B无害的语法糖吗?将其限制为结构的设计理由是什么?

d operator-overloading design-rationale assignment-operator

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

对于const成员,如何在没有operator =()的情况下使用push_back?

如何将push_back()转换为C++ std :: vector而不使用默认定义违反const成员的operator =()?

struct Item {
  Item(int value)
    : _value(value) {
  }
  const char _value;
}

vector<Item> items;

items.push_back(Item(3));
Run Code Online (Sandbox Code Playgroud)

我想保留_value const,因为它在构造对象后不应该改变,所以问题是如何在不调用operator =()的情况下用元素初始化我的向量?

这是g ++ v3.4.6给我的基本错误:

.../3.4.6/bits/vector.tcc: In member function `Item& Item::operator=(const Item&)':
.../3.4.6/bits/vector.tcc:238:   instantiated from `void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Item, _Alloc = std::allocator<Item>]'
.../3.4.6/bits/stl_vector.h:564:   instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Item, _Alloc = std::allocator<Item>]'
item.cpp:170:   instantiated from here
.../3.4.6/bits/vector.tcc:238: error: non-static const member `const char Item::_value', can't …
Run Code Online (Sandbox Code Playgroud)

c++ gcc const stdvector assignment-operator

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