现在的位置: 首页 > 综合 > 正文

拷贝构造函数、explicit关键字

2013年09月03日 ⁄ 综合 ⁄ 共 510字 ⁄ 字号 评论关闭

http://baike.baidu.com/view/1266959.htm

http://blog.csdn.net/fornormandy/article/details/79512

 

#include <iostream>
#include <stdio.h>
using namespace std;

class C
{

public:
    int i;
//explicit C(const C& arg):i(10)
   C(const C& arg)  // an explicit copy constructor
   {
    i = 10;
      printf("in the copy constructor\n");
   }
   explicit C(int i )   // an explicit constructor
   {
      printf("\nin the constructor");
   }
   C()
   {
      i = 0;
   }
};

C f(C c)
{
    // C2558
   c.i = 2;
   printf("%d\n",c.i);
   return c;   // first call to copy constructor
}

int main()
{
   C c;
   c = f(c);   // c is copied 临时拷贝
   printf("%d\n",c.i);
}

explicit关键字禁止隐式转换、禁止拷贝构造

抱歉!评论已关闭.