explicit关键字与拷贝构造及赋值


今天看到公司的代码内有大量的explicit关键字,但是老版的MSDN内例子并不完善,实在是不明白,最终从网上一篇文章内找到了答案:原来explicit是为了防止隐式使用拷贝构造函数的.以下附上从新版MSDN中找到的例子和网上那篇文章:

// Copy From MSDN

This keyword is a declaration specifier that can only be applied to in-class constructor declarations. An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object.

The following program will fail to compile because of the explicit keyword. To resolve the error, remove the explicit keywords and adjust the code in g.

// spec1_explicit.cpp
// compile with: /EHsc
#include 
class C 
{
public:
   int i;
   explicit C(const C&)   // an explicit copy constructor
   {
      printf("/nin the copy constructor");
   }
   explicit C(int i )   // an explicit constructor
   {
      printf("/nin the constructor");
   }


   C()
   {
      i = 0;
   }
};

class C2
{
public:
   int i;
   explicit C2(int i )   // an explicit constructor
   {
   }
};

C f(C c)
{   // C2558
   c.i = 2;
   return c;   // first call to copy constructor
}

void f2(C2)
{
}

void g(int i)
{
    f2(i);   // C2558
   // try the following line instead
   // f2(C2(i));
}

int main()
{
   C c, d;
   d = f(c);   // c is copied
}

Note   explicit on a constructor with multiple arguments has no effect, since such constructors cannot take part in implicit conversions. However, for the purpose of implicit conversion, explicit will have an effect if a constructor has multiple arguments and all but one of the arguments has a default value.

 

// Copy From Internet Article

Pointer
不看书不知道自己的C++有多差T_T,看到 explicit 时遇到一个问题。请看下面一段程序:
class A{
public:
A(int i) : m_i(i){}
int m_i;
};
int main(){
A a = 0;
a = 10; // 这里是什么操作?
}

这个操作产生了一个临时对象。
我怀疑是默认赋值运算符 “A &operator = (int i){}”,于是重载了一下该运算符,结果确实运行到重载的运算符函数里了,那么临时对象又是如何产生的呢?

难道默认的赋值运算符是这样操作的?
A &operator = (int i){
A a(i);
return a;
}

这让我想起了类似的函数操作:
void fn(A a){
// ...
}
这里可以直接写fn(10);也是产生了一个临时对象。

难道真的是这样吗?忘解惑。


alexeyomux
老兄你用的是哪个编译器?在我印象之中,好像标准C++并不会给出operator =啊。等我去试一试。

gongminmin
当然会有默认的operator=了
按照c++标准,编译器会生成五个默认成员函数:
默认构造函数
拷贝构造函数
析构函数
operator=
operator&
 
千里马肝
class A
{
public:
A(int i) : m_i(i){}
int m_i;
};

分别说说吧:
1. A a = 0;
首先, compiler认为这样写是不符合规矩的, 因为A = A才是正常行为。
但是她并不放弃, 通过搜索, 发现A可以根据一个int构造, 同时这个A(int i)没有用explicit修饰过。
那么A a = 0; 这样的一句话随即转变成:
A tmp(0);
A a = tmp;
需要说明的是, A a = tmp是调用的copy ctor, 虽然class A中并没有, 但是通常不写copy ctor的话,
compiler都会生成一个memberwise assignment操作性质的ctor, 底层实现通常会以memcpy进行。

2. a = 10;
首先, 这样同ctor的情况一样, compiler无法直接进行操作。
类推, 等同于代码:
A tmp(10);
a = tmp;
需要注意的是, a = tmp是调用的assignment操作, 同ctor一样,我们自己不写, 编译器同样进行
memberwise assignment操作。

3. fn(A a)
同样, fn(10)也是不对的, 但是"按照惯例", 呵呵, 会有:
A tmp(10);
fn(tmp); 

另外, 为你解惑:
copy ctor的写法只能是T::T(const T &);
而assignment的写法可以多变, 即任意. 以T为例, 
可以有
T &operator = (int n);
也可有
T &operator = (const char *);
当然, 你要确认如此的定义是对T而言有意义.

然后, 上述a = tmp, 即调用的默认的、标准的、自动生成的T &operator = (const T &). 
开销是会有一个临时的A tmp生成, 然后memcpy.
但如果你自已写了T &operator = (int n), 那么a = 10即意味着a.m_i = 10. 
当然, 以开销而言要视你的T &operator = (int n)是否为inline了.

对于explicit, 当修饰explicit A(int i) : m_i(i){}, 那么即告诉compiler不要在私底下做那么多的转换动作.
而且自动生成如A tmp(0)这样的东西是我们不想要的, 因为某些情况下自动转换这种行为是错误的.

最后, 相关此类问题, 还有一个话题, 即class A可以有operator int(), 会在
fn(int n){}
A a(3);
fn(a)
起到魔术般的作用. 关于这个, 留给你自己看看书吧:)

原文链接: https://www.cnblogs.com/chingliu/archive/2011/05/09/2223826.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    explicit关键字与拷贝构造及赋值

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/25216

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午3:04
下一篇 2023年2月8日 上午3:05

相关推荐