标准数据之间会进行  隐式类型安全转换。

  转换规则:

  

隐式类型转换的问题:  

#include <iostream>
#include <string> using namespace std; int main()
{
short s = 'a';
unsigned int ui = ;
int i = -;
double d = i; cout <<"d = "<<d<<endl;//输出d = -2000
cout <<"ui= "<<ui<<endl;//输出 ui = 1000
cout <<"ui+i = "<<ui+i<<endl;//等价于 unsigned int ui +(unsigned int)i = ? }
 cout <<"ui+i = "<<ui+i<<endl;//等价于 unsigned int ui +(unsigned int)i = ?

根据:-2000 + 1000 应该等于-1000,但是这里编译器帮我们做了隐式类型转换将  int 类型的变量 i 隐式类型转换为 unsigned int i 然后进行 i 与 ui 相加。所以出现了问题。

普通类型与类类型进行强制类型转换。  

  使用转换构造函数。

#include <iostream>
#include <string> using namespace std; /*
使用普通类型强制转换为 类类型。
转换构造函数:
-有仅有一个参数
-参数是基本类型
-参数是其它类型
使用explicit 关键字。
*/
class Test
{
int mvalue;
public:
int getValue()
{
return mvalue;
}
Test ()
{
mvalue = ;
}
explicit Test(int i)//转换构造函数
{
mvalue = i;
}
}; int main()
{
Test t; cout << "t.getValue() = " <<t.getValue() <<endl;//输出t.getValue() = 0
// t = 5;//这里等价于 t= Test(5);
// Test tt= Test(5);
t = static_cast<Test>();
cout << "t.getValue() = " <<t.getValue() <<endl;//输出t.getValue() = 5
t = Test();
cout << "t.getValue() = " <<t.getValue() <<endl;//输出t.getValue() = 10
return ;
}

运行结果:

t.getValue() =
t.getValue() = 5 t.getValue() = 10
 

  在实际工程中使用explicit 关键字杜绝编译器的转换尝试。

  转换构造函数被explicit 修饰只能进行显示转换

转换方式:

  static_cast<className>(value);

  className(value);例如:t= Test(5);

将类类型转换为普通类型:

  C++ 中可以定义类型转换函数,用作将类转换为其它类型。

  语法规则:

    operator Type()

      {

        Type ret ;

        ........

        return ret;

       }

#include <iostream>
#include <string> using namespace std; /*
将类类型转换为普通类型: */
class Test
{
int mvalue;
public:
int getValue()
{
return mvalue;
}
Test ()
{
mvalue = ;
}
Test(int i)
{
mvalue = i;
}
operator int()
{
return mvalue;
}
}; int main()
{
Test t();
int i = t;
cout << "i = "<< i<<endl;//输出i = 100
return ;
}

  

类类型互相转换:

  定义类型转换函数

#include <iostream>
#include <string> using namespace std; /*
类类型互相转换 */ class Test;
class value
{
int mvalue;
public:
int getValue()
{
return mvalue;
}
value(int i)
{ mvalue= i;
}
explicit value(Test & t)
{ }
};
class Test
{
int mvalue;
public:
int getValue()
{
return mvalue;
}
Test ()
{
mvalue = ;
}
Test(int i)
{
mvalue = i;
}
operator int()//类型转换函数
{
return mvalue;
}
value tovalue ()//类型转换函数 ,工程中以Type toType() 的公有成员函数代替。
{
// value ret(this->getValue());
value ret(mvalue); cout << "operator value ()"<<endl;
return ret;
}
}; int main()
{
Test t();
int i = t;
cout << "i = "<< i<<endl; value tt = t.tovalue();
cout << tt.getValue()<<endl;
return ;
}

类型转换函数:

  -无法抑制隐式的类型转换函数调用。

  -类型转换函数可能与转换构造函数冲突。

  -工程中以Type toType() 的公有成员函数代替。

C++ 类类型转换函数explicit 关键字的更多相关文章

  1. 【C++】隐式类型转换和 explicit关键字

    来自:黄邦勇帅 1. 当类中带有一个参数的构造函数时,将执形对象的隐式转换,比如有类 A,有一个带有一个参数的构造函数A(int i){}则当出现语句A m=1;就会调用带有一个参数的构造函数来创建对 ...

  2. 21.C++- "++"操作符重载、隐式转换之explicit关键字、类的类型转换函数

    ++操作符重载 ++操作符分为前置++和后置++,比如: ++a;  a++; ++操作符可以进行全局函数或成员函数重载 重载前置++操作符不需要参数 重载后置++操作符需要一个int类型的占位参数 ...

  3. extends 类的继承 / super关键字,调用继承类里面的函数和变量

    Son 继承Father 当其他脚本想调用 Father类里面的变量 or 方法 可以把 Son r=new Son()   等价于 Father r=new Father() 注意: 函数只能单继承 ...

  4. C++中explicit关键字的使用

    看书看到了explicit关键字,就来做个笔记,讲得比较明白,比较浅. 在C++中,我们有时可以将构造函数用作自动类型转换函数.但这种自动特性并非总是合乎要求的,有时会导致意外的类型转换,因此,C++ ...

  5. C++ 隐式类类型转换

    <C++ Primer>中提到: “可以用 单个形参来调用 的构造函数定义了从 形参类型 到 该类类型 的一个隐式转换.” 这里应该注意的是, “可以用单个形参进行调用” 并不是指构造函数 ...

  6. C++中explicit关键字的作用

    转载自:http://www.cnblogs.com/winnersun/archive/2011/07/16/2108440.html explicit用来防止由构造函数定义的隐式转换. 要明白它的 ...

  7. 《More Effective C++》 条款5 谨慎定义类型转换函数

    ---恢复内容开始--- C++编译器能够在两种数据类型之间进行隐式转换(implicit conversions),它继承了C语言的转换方法,例如允许把char隐式转换为int和从short隐式转换 ...

  8. C++explicit关键字

    在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换. explicit使用注意事项: *     explicit  ...

  9. C++中explicit关键字的作用 (转)

    explicit用来防止由构造函数定义的隐式转换. 要明白它的作用,首先要了解隐式转换:可以用单个实参来调用的构造函数定义了从形参类型到该类类型的一个隐式转换. 例如: class things { ...

随机推荐

  1. iPhone/iPad调整事件递交

    UIKit 为应用程序提供了编程手段来简化事件处理或者完全关闭事件流.下面的列表总结了这些方法: 关闭触摸事件的递交. 缺省情况下,视图接收触摸事件,但是你可以设置它的userInteractionE ...

  2. vscode-icons插件使用

    1.作用 可以使VScode左侧的资源管理器根据文件类型显示图标 2.下载 3.配置 点击菜单选项:File->Preferences->File Icon Theme 选择VSCode ...

  3. @Transient的应用

    我今天分配的任务是为一个页面Debug,遇到了一个问题查询的实体类在数据库没有对应的表,这时最常用的是建立视图或者表,但是应用@Transient注释可以让你更简单,免除建立表还有视图需要找多表关联关 ...

  4. sentinel.conf样例

    #禁止用localhost与127.0.0.1# Example sentinel.conf # 哨兵sentinel实例运行的端口 默认26379port 26379 # 哨兵sentinel的工作 ...

  5. 使用谷歌提供的SwipeRefreshLayout下拉控件,并自定义实现下拉加载的功能

    package com.loaderman.swiperefreshdemo; import android.os.Bundle; import android.os.Handler; import ...

  6. Django之缓存配置

    01-什么是缓存 缓存(cache),其作用是缓和较慢存储的高频次请求,简单来说,就是加速满存储的访问效率. 02-几种缓存配置 # 内存缓存:local-memory caching CACHES ...

  7. SqlServer Where后面Case When的使用实例

    SqlServer一个(用户表:a)中有两个字段都是用户ID 第一个ID是(收费员:id_remitter) 第二个ID是(退费员:id_returner) (收费表:b) 如何根据是否退费(F_RE ...

  8. ajax post 请求

    $(".login_btn").click(function(){ if($(".user_").val()=="admin"&&a ...

  9. SQL学习(三)Select语句:返回前多少行数据

    在实际工作中,我们可能根据某种排序后,只需要显示前多少条数据,此时就需要根据不同的数据库,使用不同的关键字 一.SQL Server/Access select top 数量/百分比 from tab ...

  10. Spring学习之==>IoC

    一.概述 Spring的三大核心思想:IoC(控制反转),DI(依赖注入),AOP(面向切面编程).本问讲着重介绍一下控制反转. 何谓控制反转:Spring 通过一种称作控制反转(IoC)的技术促进了 ...