请确认我是否正确并告诉我是否有更好的解决方案:
我理解具有常量成员的对象int const width;不能由编译器隐式创建的合成赋值运算符处理.但是QList(我想std :: list)也需要一个工作赋值运算符.因此,当我想使用具有常量成员和QList的对象时,我有三种可能性:
那是对的吗?还有其他优雅的解决方案?
我也想知道我是否可以:
编辑:我自己从不分配这个类的对象.它们仅由复制构造函数或重载的构造函数创建.因此,只有容器才需要赋值操作符而不是我自己.
EDIT2:这是我创建的赋值运算符.我不确定它是否正确.Cell有两个参数构造函数.这些参数使用初始化列表设置两个常量成员.但该对象还包含其他变量(非常量)成员.
Cell& Cell::operator=(Cell const& other)
{
if (this != &other) {
Cell* newCell = new Cell(other.column(), other.row());
return *newCell;
}
return *this;
}
Run Code Online (Sandbox Code Playgroud)
EDIT3:我发现这个帖子几乎有同样的问题:C++:STL麻烦与const类成员所有答案结合在一起回答了我的问题.
我可以将指针分配给整数变量吗?如下所示。
int *pointer;
int array1[25];
int addressOfArray;
pointer = &array1[0];
addressOfArray = pointer;
Run Code Online (Sandbox Code Playgroud)
有可能这样做吗?
我了解到这i+=2是缺点i=i+2.但现在我怀疑它.对于以下代码,上述知识没有任何好处:
byte b=0;B = B + 2; //错误:必需字节,找到int
上面的代码是合理的,因为2是int类型和表达式返回int值.
但是,以下代码运行正常:
byte b=0; b+=2; //b stores 2 after += operation
这迫使我怀疑+=短手操作员比我所知道的更多.请赐教.
在c ++中,当类包含动态分配的数据时,显式定义复制构造函数operator =和destructor通常是合理的.但这些特殊方法的活动重叠.更具体地说,operator =通常首先进行一些破坏,然后它会复制类似于复制构造函数中的那个.
我的问题是如何在不重复相同代码行的情况下以最佳方式编写此代码,而无需处理器执行不必要的工作(如不必要的复制).
我通常最终得到两种帮助方法.一个用于施工,一个用于破坏.第一个是从复制构造函数和operator =调用的.第二个是析构函数和operator =使用的.
这是示例代码:
template <class T>
class MyClass
{
private:
// Data members
int count;
T* data; // Some of them are dynamicly allocated
void construct(const MyClass& myClass)
{
// Code which does deep copy
this->count = myClass.count;
data = new T[count];
try
{
for (int i = 0; i < count; i++)
data[i] = myClass.data[i];
}
catch (...)
{
delete[] data;
throw;
}
}
void destruct()
{
// Dealocate all dynamicly allocated data …Run Code Online (Sandbox Code Playgroud) c++ destructor copy-constructor code-sharing assignment-operator
我有一个c ++类来处理分数,我希望它允许转换为double,
我有类似的东西:
class fraction
{
double n,d;
public:
fraction(double _n, double _d) {n = _n; d = _d;}
//some functions
double todouble() {return n/d;}
};
fraction frac(1,2);
double dbl = frac.todouble();
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,但我想重载赋值运算符,所以我可以直接使用:
double dbl = frac;
Run Code Online (Sandbox Code Playgroud)
我试着添加这个:
double double::operator =(double& dbl, fraction& frac) {return dbl = frac.n / frac.d;}
Run Code Online (Sandbox Code Playgroud)
这导致以下编译错误:
error: ‘double fraction::operator=(double&, fraction&)’ must take exactly one argument
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我正在读的书说,当您的类包含一个引用或const成员时,使用编译器生成的复制构造函数或赋值运算符将不起作用。例如,
#include <iostream>
#include <string>
using namespace std;
class TextBlock
{
public:
TextBlock (string str) : s(str) {
cout << "Constructor is being called" << endl;
}
string& s;
};
int main () {
TextBlock p("foo");
TextBlock q(p);
q = p;
cout << "Q's s is " << q.s << endl;
return(0);
}
Run Code Online (Sandbox Code Playgroud)
根据我的书,这两行TextBlock q(p);和q = p;都应返回编译器错误。但是使用Linux的g ++编译器时,我只会看到一行错误。q = p;当我将其注释掉时,这可以正常工作并且代码可以编译。正确的s输出给Q,因此显然是由编译器生成的副本构造函数复制的。将行更改为时string& s;,我得到相同的结果const string s。
C ++是否进行了一些更改,现在允许自动为引用和const对象生成复制构造函数,但不允许赋值运算符生成?还是我只是不正确地理解这本书?有什么想法吗?
c++ compiler-construction copy-constructor assignment-operator
我正在阅读有关复制控制的内容,并在C++ Primer(第13.4章)一书中看到了以下示例.
我的问题是关于remove_from_Folders();内部复制赋值运算符:
如果我们首先这样做remove_from_Folders();,在自我赋值的情况下,它不folders.clear();清除rhs的数据成员并导致失败?
Run Code Online (Sandbox Code Playgroud)Message& Message::operator=(const Message &rhs) { // handle self-assignment by removing pointers before inserting them remove_from_Folders(); // update existing Folders contents = rhs.contents; // copy message contents from rhs folders = rhs.folders; // copy Folder pointers from rhs add_to_Folders(rhs); // add this Message to those Folders return *this; } // remove this Message from the corresponding Folders void Message::remove_from_Folders() { for (auto f : folders) // for each pointer …
在c ++ 11中,我们可以使用delete禁用复制构造函数和赋值运算符:
class A {
A(const A&) = delete;
A& operator=(const A&) = delete;
}
Run Code Online (Sandbox Code Playgroud)
有一天,我的同事使用void返回类型而不是引用。
class A {
A(const A&) = delete;
void operator=(const A&) = delete;
}
Run Code Online (Sandbox Code Playgroud)
这个还可以吗?
例如,如果我有
A a, b, c;
a = b = c;
Run Code Online (Sandbox Code Playgroud)
这会工作吗?
对于类型为T的类,编译器可以生成以下成员,具体取决于类:
T::T()T::T(const T&)T::T(T&&)T& T::operator=(const T&)T& T::operator=(T&&)在C++ 14和C++ 17中,哪些规则导致constexpr编译器生成这些函数的版本?
c++ ×6
c ×1
c++11 ×1
c++14 ×1
c++17 ×1
class ×1
code-sharing ×1
constexpr ×1
constructor ×1
destructor ×1
java ×1
overloading ×1
pointers ×1
qlist ×1
qt ×1
r ×1
shorthand ×1