- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我搜索过但找不到“何时”使用它们的答案。我一直听说它很好,因为它为我节省了额外的拷贝。我到处把它放在我上过的每个类(class)中,但有些人似乎对某些类(class)没有意义:我已经阅读了无数关于 LValues 和 RValues 以及 std::move vs. std::copy vs. memcpy 的教程与 memmove 等。甚至阅读 throw() 但我也不确定何时使用它。
我的代码如下:
struct Point
{
int X, Y;
Point();
Point(int x, int y);
~Point();
//All my other operators here..
};
然后我有一个类数组(类似 RAII 的东西):
class PA
{
private:
std::vector<Point> PointsList;
public:
PA();
//Variadic Template constructor here..
~PA();
//Operators here..
};
我应该使用 move 构造函数和复制构造函数吗?我在 Point Class 中有它,但感觉很奇怪,所以我将其删除。然后我在 PA 课上有了它,但我认为它不会做任何事情,所以我也删除了它。然后在我的位图类中,我的编译器提示有指针成员但没有重载,所以我做了:
//Copy Con:
BMPS::BMPS(const BMPS& Bmp) : Bytes(((Bmp.width * Bmp.height) != 0) ? new RGB[Bmp.width * Bmp.height] : nullptr), width(Bmp.width), height(Bmp.height), size(Bmp.size), DC(0), Image(0)
{
std::copy(Bmp.Bytes, Bmp.Bytes + (width * height), Bytes);
BMInfo = Bmp.BMInfo;
bFHeader = Bmp.bFHeader;
}
//Move Con:
BMPS::BMPS(BMPS&& Bmp) : Bytes(nullptr), width(Bmp.width), height(Bmp.height), size(Bmp.size), DC(0), Image(0)
{
Bmp.Swap(*this);
Bmp.Bytes = nullptr;
}
//Assignment:
BMPS& BMPS::operator = (BMPS Bmp)
{
Bmp.Swap(*this);
return *this;
}
//Not sure if I need Copy Assignment?
//Move Assignment:
BMPS& BMPS::operator = (BMPS&& Bmp)
{
this->Swap(Bmp);
return *this;
}
//Swap function (Member vs. Non-member?)
void BMPS::Swap(BMPS& Bmp) //throw()
{
//I was told I should put using std::swap instead here.. for some ADL thing.
//But I always learned that using is bad in headers.
std::swap(Bytes, Bmp.Bytes);
std::swap(BMInfo, Bmp.BMInfo);
std::swap(width, Bmp.width);
std::swap(height, Bmp.height);
std::swap(size, Bmp.size);
std::swap(bFHeader, Bmp.bFHeader);
}
这是正确的吗?我做错事了吗?我需要 throw() 吗?我的赋值和 move 赋值运算符实际上应该是一样的吗?我需要复制作业吗?啊这么多问题 :c 我问的最后一个论坛无法回答所有问题,所以我很困惑。最后我应该对字节使用 unique_ptr 吗? (这是一个字节/像素数组。)
最佳答案
Scott Meyer's blog 上有一些很棒的想法:
First, not all copy requests can be replaced by moves. Only copy requests for rvalues are eligible for the optimization. Second, not all types support move operations that are more efficient than copying operations. An example is a std::array. Third, even types that support efficient move operations may support them only some of the time. Case in point: std::string. It supports moves, but in cases where std::string is implemented using SSO (the small string optimization), small strings are just as expensive to move as to copy!
或许,您可以相应地对类型进行分类,然后决定哪些类型需要 move 语义。请注意,编译器自动生成 move ctors/赋值运算符存在限制,因此建议您牢记这些。当您明确指定 move 成员时,这会有所帮助。
对于没有明确指定 move 成员的类,有一些麻烦的地方。还存在显式/隐式删除 move 成员的问题,该成员禁止从右值 进行复制。可以在 Stroustrup 题为 To Move or Not to Move 的论文中找到关于隐式生成 move 成员的问题的一个非常有值(value)的来源。 .
关于 move 语义的异常处理,我建议 Dave Abraham 的帖子 Exceptionally Moving .
我会在有空的时候试着用一些例子来回答这个问题。目前,希望上述链接能帮助您入门。
关于c++ - 何时使用 move 构造函数/赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11077103/
你能解释一下这个作业是如何完成的吗, var fe, f = document.forms[0], h; 哪个等于哪个。 最佳答案 以上等同于 var fe; var f = document.for
据我测试,这两种方法都有效,但我不知道哪一种最好,也不知道它们之间的区别,这就是我想知道的。 以下是两种方法: window.location = 'http://www.google.com'; w
我正在处理用字符串填充的 numpy 数组。我的目标是分配给第一个数组 a 的切片,值包含在较小尺寸的第二个数组 b 中。 我想到的实现如下: import numpy as np a = np.em
在我使用过的其他语言(如 Erlang 和 Python)中,如果我正在拆分字符串并且不关心其中一个字段,我可以使用下划线占位符。我在 Perl 中试过这个: (_,$id) = split('
我认为这似乎很简单,但我对调用、应用、绑定(bind)感到困惑。等等 我有一个事件监听器 red.addEventListener("click", function() { j = 0;
这个问题在这里已经有了答案: What is the python "with" statement designed for? (11 个答案) 关闭 7 年前。 使用有什么区别: iFile =
这个问题在这里已经有了答案: What is the python "with" statement designed for? (11 个答案) 关闭 7 年前。 使用有什么区别: iFile =
几周前我们开始写一篇关于 Haskell 的论文,刚刚接到我们的第一个任务。我知道 SO 不喜欢家庭作业问题,所以我不会问怎么做。相反,如果有人能将我推向正确的方向,我将不胜感激。鉴于它可能不是一个特
我正在尝试为我的函数的变量根分配一个值,但似乎不起作用。我不明白这个问题。 hw7.c:155:7:警告:赋值使指针来自整数而不进行强制转换[默认启用] root = 负载(&fp, 大小); 此代码
我昨天花了大约 5 个小时来完成这个工作,并使用这个网站的帮助让代码可以工作,但我认为我这样做的方式是一种作弊方式,我使用了 scanf 命令。无论如何,我想以正确的方式解决这个问题。多谢你们!哦,代
我需要一些帮助来解决问题。 我有这个文本文件: 我将文本内容输入到字符串二维数组中,并将其转换为整数二维数组。当我转换为 int 数组时,nan 被替换为零。现在,我继续查找二维数组中每行的最大值和最
假设我有一个只能移动的类型。我们停止现有的默认提供的构造函数,但 Rvalue 引用引入了一种新的“ flavor ”,我们可以将其用于签名的移动版本: class CantCopyMe { priv
假设我有两个简单的对象,我想创建第三个对象来连接它们的属性。这非常有效: (()=>{ const a1 = {a: 2, b: 3} const b1 = {a: 100, c: 5}
我想知道我是否可以稍后在这样的代码中为 VAR 赋值 var myView: UIView func createView() { myView = UIView() { let _view =
我遇到了一些 Javascript/HTML/CSS 代码的问题。我对创建网站还很陌生,所以请多多包涵。 我最终想做的是从 javascript 中提取一个动态值并使用它对一些 div(在容器中)进行
#include class Box{ public: int x; Box(){ x=0; std::cout No move construction thanks to RV
我发现在javascript中&=运算符是按位赋值: var test=true; test&=true; //here test is an int variable javascript中是否存在
请帮助完成赋值重载函数的执行。 这是指令: 赋值运算符 (=),它将源字符串复制到目标字符串中。请注意,目标的大小需要调整为与源相同。 加法 (+) 和赋值 (=) 运算符都需要能够进行级联运算。这意
我有一个名为 SortedArrayList 的自定义结构它根据比较器对其元素进行排序,我想防止使用 operator[] 进行分配. 示例: 数组列表.h template class Array
我是 python 的新手,我看到了这种为列表赋值的形式 color= ['red' if v == 0 else 'green' for v in y] 但是如果我尝试用 3 个数字来做,例如 co
我是一名优秀的程序员,十分优秀!