码迷,mamicode.com
首页 > 编程语言 > 详细

c++类型转换

时间:2016-04-14 13:44:57      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

1、隐身转换

  

short a=2000;
int b;
b=a;     //b= (int)a;



#include <iostream>
using namespace std;

class A {};

class B {
public:
  // conversion from A (constructor):
  B (const A& x) {}
  // conversion from A (assignment):
  B& operator= (const A& x) {return *this;}
  // conversion to A (type-cast operator)
  operator A() {return A();}
};

int main ()
{
  A foo;
  B bar = foo;    // calls constructor
  bar = foo;      // calls assignment
  foo = bar;      // calls type-cast operator
  return 0;
}

dynamic_cast <new_type> (expression)      
reinterpret_cast <new_type> (expression)
static_cast <new_type> (expression)
const_cast <new_type> (expression)

typeid().name

c++类型转换

标签:

原文地址:http://www.cnblogs.com/hinice/p/5390519.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!