C++ explicit和implicit

一、类类型转换

首先,明确什么是类类型转换,

内置类型存在定义了几种自动转换的规则,同样地,类也有定义隐式的转换规则。

若构造函数没有声明explicit且只接受一个实参,则它可以进行隐式的类类型转换。(如何将一种类类型转换为另一种类类型的转换规则)

类的构造函数默认是implicit的,可以进行隐式类类型转换的

explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函数是显示的

二、隐式类类型转换

首先,引入一个Person类。

#pragma once
#include
#include
#include
using std::string;
using std::rand;
using std::cout;
using std::endl;
class Person
{
public:
	Person(string str);
	Person(string std, int a);
	~Person();
	void play_with(const Person& p);

private:

	string name;
	int age;
};

inline Person::Person(string str)
{
	this->name = str;
	srand((unsigned int)(time(NULL)));
	age = rand() % 100;
}
//inline 会在编译的时候将函数内部替换掉,从而减少函数调用的时间
 inline  Person::Person(string str, int a)
{
	this->name = str;
	this->age = a;
}
 inline void Person::play_with(const Person& p)
 {
	 cout << this->name << " is playing whih " << p.name;
 }
Person::~Person()
{
}

然后进行调用。

  1. xiaom初始化的时候,使用的是先构造函数这里进行了隐式转换,直接调用的构造函数不会调用拷贝构造函数。
  2. play_with(b)运行的时候,编译器会用给定的string自动创建一个Persion对象,作为临时对象传给paly_with。
  3. 对于xiaoc的初始化,是将c的ASCII值作为int型。
#include 
#include"Explicti.h"
int main()
{ 
    string a="xiaoming"; Person xiaom = a;//隐式转换1 
    string b = "xiaohong"; 
    xiaom.play_with(b);//隐式转换2 这里第一个参数的是一个const类型的引用 
    Person xiaoc = 'c';//隐式转换3
}

注意:

  1. 必须一步转换
    //将char字符数组先转为string,再转换为Person,错误 
    xiaom.play_with("xiaohong");
    
    //先显示转换string,隐式转换为类类型
    xiaom.play_with(string("xiaohong"));
      
    //隐式转换为string,再显示转换为类类型
    xiaom.play_with(Person("xiaohong"));
  2. 默认不声明,是可以隐式的。
  3. 需要多个参数的构造函数不能用于隐式转换;但是也有例外,当多参数构造函数除了第一个以外都有默认实参的时候,可以对第一个参数进行隐式转换。

三、显示类类型转换

在声明中添加explicit

//在声明中添加explicit	
explicit Person(string str);

string a="xiaoming"; Person xiaom = a;//隐式转换1  不可以转换 
string b = "xiaohong"; xiaom.play_with(b);//隐式转换2  不可以转换 
Person xiaoc = 'c';//隐式转换3  仍然可以

当然即使有explicit我们也可以显示的转换。

string a = "xiaoming"; 
Person xiaom = static_cast(a);//隐式转换1 
string b = "xiaohong"; 
xiaom.play_with(static_cast(b));//隐式转换2 

注意:

  1. 在类内部声明,不能在类外部定义的时候不应该重复
    explicit inline Person::Person(string str)
    {
    	this->name = str;
    	srand((unsigned int)(time(NULL)));
    	age = rand() % 100;
    }
  2. explicit的构造函数只能用于直接初始化。 (除非你提取进行了显示转换)
  3. explicit只对有一个参数构造函数有效;

但是也有例外,当多参数构造函数除了第一个以外都有默认实参的时候,explicit有效。

四、类型转换运算符

既然存在将实参类型转换为类类型,那么也能定义将类类型转换为其他类型,这就是类型转换运算符。

类型转换运算符是类的一种特殊成员函数,它负责将一个类类型的值转换成其他类型。

类型转换运算符

operator type() const;

这里type可以面向任何可以作为函数返回类型的类型。因此,不允许转换成数组或函数类型,但是允许转换为指针(包括数组指整和函数指针)或者引用类型。

类型转换运算符不应该改变待转换对象的内容,一般被定义成const成员。

对于Person类,将person类转换为string类型。

operator string () const;

inline Person::operator string() const
{ return this->name + std::to_string(this->age);
}

string res = xiaom ; cout << res << endl;

这里会将Person类转换为string类型。

注意:

  1. 类型转换运算符不能指定返回类型。
  2. 参数列表需要为空
  3. 类型转换运算符的定义中的返回类型需要时type类型。
    inline Person::operator int *() const
    { 
        return 42;//这是错误的,不能正常转换为int指针类型
    }
  4. 一般声明为const成员。