c so 反射 c 實現反射機制

2021-10-12 14:11:05 字數 916 閱讀 4253

下午接了個阿里**面試,當時剛剛睡醒,感覺有點懵。。。大腦莫名當機狀態,很多問題沒有表述清楚,有乙個關於 c++ 如何實現反射機制的問題,感覺蠻有意思,當時雖然回答了用函式指標和工廠模式,但是表述並不當,細節也沒有想清楚。。。 晚上抽空簡單實現了一發:

> file name: reflector.cpp

> author: xtestw

> mail: [email protected]

> created time: fri mar 11 21:09:12 2016

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

typedef void* (*getclz)(void);

map clzs;

class t1{

public:

t1(){

i=j=1;

static void* getclz(){

return new t1();

int i,j;

class t2{

public:

t2(){

i=j=2;

static void* getclz(){

return new t2();

int i,j;

int main(){

clzs["t1"]=t1::getclz;

clzs["t2"]=t2::getclz;

t1* t=(t1 *)clzs["t1"]();

coutreturn 0;

c so 反射 C 實現反射機制

net下的很多技術都是基於反射機制來實現的,反射讓.net平台下的語言變得得心應手。最簡單的,比如列舉型別,我們我可以很容易的獲得乙個列舉變數的數值以及其名稱字串。可是,在c 中,列舉變數本質上和乙個整形變數沒有區別,我們很難獲取乙個列舉變數的名稱字串。其實在c 中,我們可以通過巨集來實現類似反射的...

c so 反射 C 反射的方法與實現

1.什麼是反射?反射是程式獲取自身資訊的能力 2.反射有什麼用?可以用於動態建立型別,跨語言跨平台資料互動,持久化,序列化等等。3.反射包含哪些功能?至少包含以下功能 列舉所有member 獲取member的name和type 能夠get set member 4.有哪些反射方法?反射主要有3種實現...

c 實現反射機制

c 中反射機制比較常見,而c 中沒有該機制。使用c 實現反射機制,主要利用了工廠模式程序物件的生產。itest.h 基類test.h 繼承類helper.h 幫助類main.cpp 主函式入口檔案 pragma once include using namespace std class itest...