- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
当我学习 C++ 时,人们告诉我要始终至少实现三种方法的规则。
现在我看到了新的“... = default;”来自 c++0x 堆栈溢出,我的问题是:
Is there a c++11 standard implementation defined for those methods or is it compiler specific?
另外我想要一些精确度:
- What does the implementation looks like in term of code? (if it's generic)
- Does this have an advantage compared to my example implementation below?
- If you don't use assignment/copy constructor, what does
*... = delete*
do precisly, what's the difference with declaring them private? Answer (from @40two)- Is the new default= different from the old default implementation?
免责声明:当我需要在我的方法中使用更多高级功能时,我肯定会自己实现它们。但是我习惯于实现赋值运算符和复制构造函数,即使我从未使用过它们,只是为了让编译器不用。
我曾经做过的事情:(编辑,@DDrmmr swap/move)
//File T.h
class T
{
public:
T(void);
T(const T &other);
T(const T &&other);
T &operator=(T other);
friend void swap(T &first, T &second);
~T(void);
protected:
int *_param;
};
//File T.cpp
T::T(void) :
_param(std::null)
{}
T::T(T &other)
: _param(other._param)
{}
T::T(T &&other)
: T()
{
swap(*this, other);
}
T &T::operator=(T other)
{
swap(*this, other);
return (*this);
}
friend void swap(T &first, T &second)
{
using std::swap;
swap(first._param, second._param);
}
T::~T(void)
{}
最佳答案
默认行为是:
T()
):调用 bases def。执行者和成员默认执行者。T(const T&)
): 调用基础复制。 Actor 和成员复制 Actor 。T(T&&)
):调用碱基移动。 Actor 和成员移动 Actor 。T& operator=(const T&)
):调用碱基赋值。和成员分配。T& operator=(T&&)
):调用基地转移,成员转移。~T()
):调用成员析构函数和基类析构函数(倒序)。对于内置类型(int 等)
由于指针也是内置类型,因此这适用于 int*
(不适用于它指向的内容)。
现在,如果你不声明任何东西,你的 T
类将只持有一个 int* ,它不拥有指向的 int,所以 T 的拷贝将只持有一个指向相同的指针诠释。这与 C++03 产生的行为相同。内置类型的默认实现移动是复制。因为类是按成员移动的(并且取决于成员是什么:只是内置函数的拷贝)
如果你必须改变这种行为,你必须连贯地做到这一点:例如,如果你想“拥有”你指向的东西,你需要
nullptr
的默认构造函数:这定义了一个我们稍后可以引用的“空状态”.
T::T() :_param() {}
T::T(int* s) :_param(s) {}
T(const T& s) :_param(s._param? new int(*s._param): nullptr) {}
~T() { delete _param; } // will do nothing if _param is nullptr
现在我们先不定义赋值,而是专注于移动:如果你不声明它,因为你声明了拷贝,它将被删除:这使得 T 对象总是被复制,即使是临时的(与 c++03 相同的行为)
但是如果源对象是临时的,我们可以创建一个空的目的地并交换它们:
T::T(T&& s) :T() { std::swap(_param, s._param); }
这就是所谓的移动。
现在赋值:在 C++11 之前 T& operator=(const T& s)
应该检查自赋值,使目标为空并接收一份尖锐的:
T& operator=(const T& s)
{
if(this == &s) return *this; // we can shortcut
int* p = new int(s._param); //get the copy ...
delete _param; //.. and if succeeded (no exception while copying) ...
_param = p; // ... delete the old and keep the copy
return *this;
}
在 C++11 中,我们可以使用参数传递来生成拷贝,从而给出
T& operator=(T s) //note the signature
{ std::swap(_param, s._param); return *this; }
请注意,这在 C++98 中也有效,但如果 s
是临时的,传递拷贝将不会在传递移动中优化。这使得这样的实现在 C++98 和 C++03 中无利可图,但在 C++11 中却非常方便。
另请注意,无需为 T 专门化 std::swap
:std::swap(a,b);
将起作用,实现为三个移动(不是复制)
实现交换函数的做法是针对 T 有很多成员的情况,在移动和分配中都需要交换。但它可以是一个普通的私有(private)成员函数。
关于c++ - c++11对于三种方法的规则如何实现 "... = default;",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24445167/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!