C++ explicit 关键字

简介:

explicit 修饰只有一个参数的构造函数,以防止从参数类型到目标类类型的隐式转换。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// stdmove.cpp -- using std::move()
#include <iostream>
#include <utility>
using  std::cout;
using  std::endl;
 
// use the following for g++4.5
// #define nullptr 0
// interface
class  Useless
{
private :
     int  n;           // number of elements
     char  * pc;       // pointer to data
     static  int  ct;   // number of objects
public :
     Useless();
     explicit  Useless( int  k);
     Useless( int  k,  char  ch);
     Useless( const  Useless & f);  // regular copy constructor
     ~Useless();
     Useless & operator=( const  Useless & f);  // copy assignment
     void  ShowObject()  const ;
};
 
// implementation
int  Useless::ct = 0;
 
Useless::Useless()
{
     cout <<  "enter "  << __func__ <<  "()\n" ;
     ++ct;
     n = 0;
     pc = nullptr;
     ShowObject();
     cout <<  "leave "  << __func__ <<  "()\n" ;
  }
 
Useless::Useless( int  k) : n(k)
{
     cout <<  "enter "  << __func__ <<  "(k)\n" ;
     ++ct; 
     pc =  new  char [n];
     ShowObject();
     cout <<  "leave "  << __func__ <<  "(k)\n" ;
}
 
Useless::Useless( int  k,  char  ch) : n(k)
{
     cout <<  "enter "  << __func__ <<  "(k, ch)\n" ;
     ++ct;
     pc =  new  char [n];
     for  ( int  i = 0; i < n; i++)
         pc[i] = ch;
     ShowObject();
     cout <<  "leave "  << __func__ <<  "(k, ch)\n" ;
}
 
Useless::Useless( const  Useless & f): n(f.n) 
{
     cout <<  "enter "  << __func__ <<  "(const &)\n" ;
     ++ct;
     pc =  new  char [n];
     for  ( int  i = 0; i < n; i++)
         pc[i] = f.pc[i];
     ShowObject();
     cout <<  "leave "  << __func__ <<  "(const &)\n" ;
}
 
Useless::~Useless()
{
     cout <<  "enter "  << __func__ <<  "()\n" ;
     ShowObject();
     --ct;
     delete  [] pc;
     cout <<  "leave "  << __func__ <<  "()\n" ;
}
 
Useless & Useless::operator=( const  Useless & f)   // copy assignment
{
     cout <<  "enter "  << __func__ <<  "(const &)\n" ;
     ShowObject();
     f.ShowObject();
     if  ( this  == &f)
         return  * this ;
     delete  [] pc;
     n = f.n;
     pc =  new  char [n];
     for  ( int  i = 0; i < n; i++)
         pc[i] = f.pc[i];
     ShowObject();
     cout <<  "leave "  << __func__ <<  "(const &)\n" ;
     return  * this ;
}
 
void  Useless::ShowObject()  const
     cout <<  "\t this="  <<  this  <<  ", ct="  << ct; 
     cout <<  ", pc=("  << n <<  ", "  << ( void *)pc <<  ", "
     if  (n == 0)
         cout <<  "(object empty)" ;
     else
         for  ( int  i = 0; i < n; i++)
             cout << pc[i];
     cout <<  " )."  << endl;
}
 
int  main()
{
     Useless obj1(10);
     Useless obj2=5;
}

说明:

//显式调用构造函数 ”explicit Useless(int k);”

Useless obj1(10);

//隐式调用构造函数 ”explicit Useless(int k);”。由于有explicit修饰,发生编译错误。

//error: conversion from ‘int’ to non-scalar type ‘Useless’ requested

//Useless obj2=5;




      本文转自FrankNie0101 51CTO博客,原文链接:http://blog.51cto.com/frankniefaquan/1934789,如需转载请自行联系原作者





相关文章
|
1月前
|
存储 安全 算法
【C/C++ 关键字 函数说明符 】C++ noexcept 关键字(指定某个函数不抛出异常)
【C/C++ 关键字 函数说明符 】C++ noexcept 关键字(指定某个函数不抛出异常)
26 0
|
1月前
|
设计模式 算法 安全
【C/C++ 关键字 函数说明符 】C++ final关键字(修饰成员函数无法被子类重写覆盖)
【C/C++ 关键字 函数说明符 】C++ final关键字(修饰成员函数无法被子类重写覆盖)
40 1
|
1月前
|
算法 安全 编译器
【C++ 关键字 override】C++ 重写关键字override(强制编译器检查该函数是否覆盖已存在的虚函数)
【C++ 关键字 override】C++ 重写关键字override(强制编译器检查该函数是否覆盖已存在的虚函数)
27 0
|
1月前
|
算法 Java 编译器
【C++ 关键字 virtual 】C++ virtual 关键字(将成员函数声明为虚函数实现多态
【C++ 关键字 virtual 】C++ virtual 关键字(将成员函数声明为虚函数实现多态
25 0
|
1月前
|
存储 缓存 安全
【C/C++ 关键字 存储类说明符 】 线程局部变量的魔法:C++ 中 thread_local的用法
【C/C++ 关键字 存储类说明符 】 线程局部变量的魔法:C++ 中 thread_local的用法
33 0
|
1月前
|
存储 安全 编译器
【C++ 关键字 类型限定符 】揭秘C++编程中的神秘元素:深入了解volatile关键字的强大作用
【C++ 关键字 类型限定符 】揭秘C++编程中的神秘元素:深入了解volatile关键字的强大作用
21 0
|
17天前
|
编译器 C语言 C++
【C++】C++入门第一课(c++关键字 | 命名空间 | c++输入输出 | 缺省参数)
【C++】C++入门第一课(c++关键字 | 命名空间 | c++输入输出 | 缺省参数)
|
17天前
|
存储 程序员 编译器
C++注释、变量、常量、关键字、标识符、输入输出
C++注释、变量、常量、关键字、标识符、输入输出
|
18天前
|
编译器 C语言 C++
【C++的奇迹之旅(二)】C++关键字&&命名空间使用的三种方式&&C++输入&输出&&命名空间std的使用惯例
【C++的奇迹之旅(二)】C++关键字&&命名空间使用的三种方式&&C++输入&输出&&命名空间std的使用惯例
|
1月前
|
存储 算法 编译器
【C++ 关键字 static_assert 相关问题】C++ 关于静态断言的编译问题 ,深入了解静态断言
【C++ 关键字 static_assert 相关问题】C++ 关于静态断言的编译问题 ,深入了解静态断言
29 0