批註:多型例項
#include <iostream>
#include <stdio.h>
using namespace std;
class Base
{
public :
virtual void Print() { cout<<"This is Base"<<endl;}
};
class Base2
{
public:
virtual void Print2() {cout <<"This is base2"<<endl;}
};
class Base3
{
private :
void Print3() {cout <<"This is base3"<<endl;}
};
class Derived: public Base,public Base2
{
public:
void Print() {cout<<"This is Derived"<<endl;}
void Print2() {cout<<"This is Derived2"<<endl;}
void Out() {cout<<"This should not be called"<<endl;}
};
typedef void (*Func)();
int main()
{
Base *pb = new Derived();
Base *p = new Base();
pb->Print();
int* pA = (int*)(*((int*)(pb+1)));
int* pAB = (int*)(*((int*)pb));
cout <<sizeof(Base)<<endl; //計算class的大小用sizeof(class), sizeof(物件)=指標大小,多大的類都等於8.
cout <<sizeof(*pb)<<endl; //在64bit機器上指標是8位元組。不同於32bit機器。
cout <<sizeof(Derived)<<endl; //在64bit機器上虛擬函式地址指標是8位元組。不同於32bit機器
cout <<sizeof(Base3)<<endl;
// it is the same as above
// Func pFunc = (Func)*pA;
int *a = (int*)pb; //get Derive address
int *b =((int*)pb+1); //get Derive print2 address
int c = *((int*)pb+1); //
// cout <<"*b="<<*b<<endl;
//it is the same as struct,add include function point
cout<<"c="<<c<<endl;
// cout <<"*d="<<*d<<endl;
Func pFunc =(Func)*pAB;
pFunc();
pFunc =(Func)*pA;
//func point (void*)p
//Func pFunc =(Func*)*pA;
pFunc();
return 0;
}
#include <stdio.h>
using namespace std;
class Base
{
public :
virtual void Print() { cout<<"This is Base"<<endl;}
};
class Base2
{
public:
virtual void Print2() {cout <<"This is base2"<<endl;}
};
class Base3
{
private :
void Print3() {cout <<"This is base3"<<endl;}
};
class Derived: public Base,public Base2
{
public:
void Print() {cout<<"This is Derived"<<endl;}
void Print2() {cout<<"This is Derived2"<<endl;}
void Out() {cout<<"This should not be called"<<endl;}
};
typedef void (*Func)();
int main()
{
Base *pb = new Derived();
Base *p = new Base();
pb->Print();
int* pA = (int*)(*((int*)(pb+1)));
int* pAB = (int*)(*((int*)pb));
cout <<sizeof(Base)<<endl; //計算class的大小用sizeof(class), sizeof(物件)=指標大小,多大的類都等於8.
cout <<sizeof(*pb)<<endl; //在64bit機器上指標是8位元組。不同於32bit機器。
cout <<sizeof(Derived)<<endl; //在64bit機器上虛擬函式地址指標是8位元組。不同於32bit機器
cout <<sizeof(Base3)<<endl;
// it is the same as above
// Func pFunc = (Func)*pA;
int *a = (int*)pb; //get Derive address
int *b =((int*)pb+1); //get Derive print2 address
int c = *((int*)pb+1); //
//d(*pA) is save Device print2 address
//int *d = (int *)(*((int*)pb+1));
int *d = (int *)(*((int*)(pb+1))); //此處改成將(pb+1)闊起來,這樣+1就不是 int的4個位元組,而是pb的單位8個位元組了 . 非常漂亮!
// cout <<"*b="<<*b<<endl;
//it is the same as struct,add include function point
cout<<"c="<<c<<endl;
// cout <<"*d="<<*d<<endl;
Func pFunc =(Func)*pAB;
pFunc();
pFunc =(Func)*pA;
//func point (void*)p
//Func pFunc =(Func*)*pA;
pFunc();
return 0;
}
相關文章
- java多型例項Java多型
- java多型抽象類例項Java多型抽象
- java多型-優化上個例項Java多型優化
- Java 介面實現多型 -- 膝上型電腦綜合例項Java多型
- js模擬實現多型效果程式碼例項JS多型
- docker redis 多個例項DockerRedis
- 例項動態註冊跟蹤
- 再談多型--多型的應用舉例: (轉)多型
- 用三個例項說明python類多型和過載。Python多型
- Python物件導向多型實現原理及程式碼例項Python物件多型
- on()方法一次註冊多個事件處理函式程式碼例項事件函式
- Word文件怎麼新增批註?Word文件批註新增教程
- Java中建立泛型型別的例項Java泛型型別
- Spring中用註解建立bean例項SpringBean
- WebGL多模型光照綜合例項Web模型
- 多個mapreduce連線例項
- java多執行緒例項Java執行緒
- 例項操作mysql varchar型別求和MySql型別
- 備忘錄——C#建立釘釘OA審批例項C#
- 【轉】java中註解的使用與例項Java
- JavaScript多級選項卡效果程式碼例項JavaScript
- oracle 多個例項監聽不到Oracle
- oracle多個例項啟動方法Oracle
- JDBC連線自定義sqlserver資料庫例項名(多個例項)JDBCSQLServer資料庫
- 簡易型PDA的開發例項
- 批註:智慧指標分析指標
- css多欄佈局程式碼例項CSS
- oracle 多個例項安裝監聽Oracle
- 註冊中心 Eureka 原始碼解析 —— 應用例項註冊發現(一)之註冊原始碼
- c#多型性測試小例C#多型
- 在例項中呼叫 Invoke 型別的類型別
- PHP弱型別引發的漏洞例項PHP型別
- oracle一個例項配置多個listener或多個埠Oracle
- 自定義註解例項實現SQL語句生成SQL
- SpringBoot+Dubbo的註解方式整合例項Spring Boot
- Linux中執行多個MySQL例項LinuxMySql
- 在Windows中執行多個MySQL例項WindowsMySql
- 基於NCF的多模組協同例項