C++數據類型轉換知識點

#include <iostream>
using namespace std;

class Father{
public :
	int time;

	virtual void run(){
	  cout<< "Fatner is running" <<endl;
	}

};

class Son : public Father{

public:
	void run(){
	  cout << "Son is plating" <<endl;
	}
};


void main(){


	//靜態轉換
	printf("%d\n",static_cast<int>(10.5));
	printf("%f\n",static_cast<float>(20));

	//指針類型轉換
	int* intp = new int(5);
	char* charp = reinterpret_cast<char*>(intp);
	for(int i = 0; i < 4; i++){
		cout<<reinterpret_cast<void*>(charp+i) << static_cast<int>(*(charp+i)) <<endl;
	}

	//涉及const必須使用const_cast
	int num[5] = {1,2,3,4,5};
	const int * p = num;
	int * pum = const_cast<int*>(p);
	
	/**
	Father father;
	Son son;
	Father* fp;
	fp = &father;
	fp->run();
	fp = &son;
	fp->run();*/

	Father* father = new Father;
	Son* son = new Son;

	Father* fp = dynamic_cast<Father*>(son);
	fp->run();

	Son* sp = dynamic_cast<Son*>(father);
	sp->run();


	cin.get();
}


基本類型 《-------》類類型

#include <iostream>

using namespace std;

class Myclass{

public:
	int x;
	int y;
	//基本類型 --》 類類型
	explicit Myclass(int a) : x(a), y(a){
	  cout << "myclass()" << endl;
	}
	//基本類型--》 類類型
	void operator = (int num){

	   cout << "operator = " << endl;	 
	   this->x = num;
	   this->y = num;
	}

	//類類型 --》 基本類型
	operator int(){
	   return x+y;
	}


};

void main(){

	Myclass myclass(10); 
	//explicct 禁止隱式轉換  但不能規避顯示轉換
	Myclass myclass2 = static_cast<Myclass>(100);
	Myclass myclass3 = Myclass(200);
	myclass3 = 200;
	int d = myclass3;
	cout << d << endl;
	cin.get();

}

類類型的數據轉換

#include <iostream>

using namespace std;

class TwoPoint{
   
public:
	int x;
	int y;
	TwoPoint(int a , int b) : x(a) , y(b){
	
	}

};

class ThreePoint{
public:
	int x;
	int y;
	int z;
	ThreePoint():x(10),y(20),z(30){
	
	}
	
	ThreePoint(const TwoPoint& teoPoint){
		this->x = teoPoint.x;
		this->y = teoPoint.y;
		this->z = 100;
	}

	operator TwoPoint(){
		return TwoPoint(x,y);
	}

	void operator = (const TwoPoint& teoPoint){
	    this->x = teoPoint.x;
		this->y = teoPoint.y;
		this->z = 100;
	}
};

void main(){


	TwoPoint two(200,300);
	ThreePoint three(two);
	cout << three.x << three.y << three.z << endl;

	ThreePoint threes;
	two = threes;
	cout << two.x << two.y << endl;

   cout << "Nanjing" << endl;
   cin.get();
}

下面就講一下dynamic_cast使用

dynamic_cast只能識別多態類型的指針和引用  必須要虛函數才能轉換


#include <iostream>


using namespace std;


class base{

public:

	virtual void show(){
		cout << "base show" << endl;
	}

};


class baseX :public base{

public:
	void show(){
		cout << "baseX show" << endl;
	}
};

void main(){

	//base* p = new baseX;
	//p->show();

	////dynamic_cast只能識別多態類型  必須要虛函數才能轉換
	//baseX* p1 = dynamic_cast<baseX*>(p); //轉換成功 依賴於virtual  沒有virtual就不是多態類型
	//p1->show();




	//base* p = new base;
	//base* p1 = new baseX;

	////多態  父類指針 可以轉化爲子類指針
	//baseX* p2 = dynamic_cast<baseX*>(p1);
	//cout << (void*)p2 << endl;


	base* p = new base;
	base* p2 = new baseX;
	baseX* p3 = new baseX;

	base& p5(*p2);

	baseX& p6 = dynamic_cast<baseX&>(p5);
	p6.show();
  


	cin.get();
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章