服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++中隐式类型转换学习笔记

C++中隐式类型转换学习笔记

2021-08-19 15:31dnsir C/C++

在本篇文章里小编给大家整理的是一篇关于C++中隐式类型转换学习笔记内容,有兴趣的跟着小编来学习下吧。

1 operator隐式类型转换

1.1 std::ref源码中reference_wrapper隐式类型转换

在std::ref的实现中有如下一段代码:

?
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
template<typename _Tp>
 class reference_wrapper
 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
 {
  _Tp* _M_data;
 
 public:
  typedef _Tp type;
 
  reference_wrapper(_Tp& __indata) noexcept
  : _M_data(std::__addressof(__indata))
  { }
 
  reference_wrapper(_Tp&&) = delete;
 
  reference_wrapper(const reference_wrapper&) = default;
 
  reference_wrapper&
  operator=(const reference_wrapper&) = default;
  //operator的隐式类型转换
  operator _Tp&() const noexcept
  { return this->get(); }
 
  _Tp&
  get() const noexcept
  { return *_M_data; }
 
  template<typename... _Args>
 typename result_of<_Tp&(_Args&&...)>::type
 operator()(_Args&&... __args) const
 {
  return __invoke(get(), std::forward<_Args>(__args)...);
 }
 };

注意看operator操作符重载:

?
1
2
operator _Tp&() const noexcept
  { return this->get(); }

就是用于类型转换。

1.2 简单的例子-实现一个class转为int的示例

?
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
#include <iostream>
/*
 *
 * c++ operator的隐式类型转换
 * 参见std::ref的实现
 */
void f(int a)
{
 std::cout << "a = " << a << std::endl;
}
class A{
 public:
 A(int a):num(a){}
 ~A() {}
 
 operator int()
 {
  return num;
 }
 int num;
};
 
int main()
{
 A a(1);
 std::cout << a + 1 << std::endl;
 f(a);
 return 0;
}

当然除了通过operator实现隐式类型转换,c++中还可以通过构造函数实现。

2 构造函数实现隐式类型转换

在c++ primer一书中提到

可以用单个实参来调用的构造函数定义了从形参类型到该类类型的一个转换

看如下示例:

?
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
#include <iostream>
/*
 *
 * c++ 构造的隐式类型转换
 * 参见std::ref的实现
 */
class B{
 public:
 B(int a):num(a){}
 ~B() {}
 
 int num;
};
 
class A{
 public:
 A(int a):num(a){}
 A(B b):num(b.num){}
 ~A() {}
 
 int fun(A a)
 {
  std::cout << num + a.num << std::endl;
 }
 
 int num;
};
 
int main()
{
 B b(1);
 A a(2);
 //通过构造函数实现了隐式类型转换
 a.fun(b); //输出结果为3
 return 0;
}

特别需要注意的是单个实参,构造函数才会有隐式转换,一个条件不满足都是不行。

3 使用explicit关键字避免构造函数隐式转换

有些时候我们并不希望发生隐式转换,不期望的隐式转换可能出现意外的结果,explicit关键词可以禁止之类隐式转换,将上述class A的构造函数改为如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
class A{
 public:
 A(int a):num(a){}
 explicit A(B b):num(b.num){}
 ~A() {}
 
 int fun(A a)
 {
  std::cout << num + a.num << std::endl;
 }
 
 int num;
};

再次运行程序出现提示:

op2.cpp: In function ‘int main()':
op2.cpp:29:12: error: no matching function for call to ‘A::fun(B&)'
a.fun(b);
^
op2.cpp:16:9: note: candidate: int A::fun(A)
int fun(A a)
^~~
op2.cpp:16:9: note: no known conversion for argument 1 from ‘B' to ‘A'

这个时候调用方式修改更改为:

?
1
2
3
4
5
6
7
int main()
{
 B b(1);
 A a(2);
 a.fun(A(b));
 return 0;
}

只能感叹C++语言的博大精深,这篇文章还只是对隐式转换的入门级总结。

参考:

《C++ Primer》隐式类类型转换学习整理

以上就是C++中隐式类型转换学习笔记的详细内容,更多关于C++中隐式类型转换的资料请关注服务器之家其它相关文章!

原文链接:https://www.jianshu.com/p/e2c794dd66e9

延伸 · 阅读

精彩推荐