我想指針存儲在一個數據結構中的不同類的對象(如陣列,HashMap的...)。據this answer我出來使用無類型指針void*
和運營商&*(ClassName*)
爲指針類型重鑄以下解決方案:存儲指針
#include <stdio.h>
class ClassA{ public: int a; };
class ClassB{ public: double b; };
class Storage{
public:
int n;
void** store;
Storage(int n_){ n = n_; store = new void*[n]; };
};
Storage s(5);
int main(){
// test store and load ClassA
ClassA a1; a1.a = 16;
s.store[ 0 ] = &a1;
ClassA* a2 = &*(ClassA*) s.store[ 0 ];
printf( " a2 %i \n", a2->a);
// test store and load ClassB
ClassB b1; b1.b = 15.1455;
s.store[ 1 ] = &b1;
ClassB* b2 = &*(ClassB*) s.store[ 1 ];
printf( " b2 %f \n", b2->b);
}
現在問題:
- 是什麼不好類似
&*(ClassB*)
的類型轉換除外,它不是類型安全的? - 是否有此類型轉換任何性能損失?
- 有沒有更好的(更好,更安全,更快)的方式如何存儲指針,以在單個陣列中不同類的不同的對象?
啊,我想通了,它實際上不是有用,使其void**
因爲比我現在有辦法以後怎麼識別存儲對象的類型。最好是用模板和普通超類來做。像這樣
#include <stdio.h>
#include <cstdlib>
template< class TYPE1 > class Storage{
public:
int n;
TYPE1** store;
Storage(int n_){ n = n_; store = new TYPE1*[n]; }; // Constructor
void put(int key, TYPE1* obj){ store[key] = obj; }; // save data
TYPE1* get(int key ){ return store[key]; }; // load data
};
class Parent { public: int kind; };
class ChildA : public Parent { public: int someData; ChildA(int someData_ ){ kind=0; someData =someData_; } };
class ChildB : public Parent { public: double otherData; ChildB(double otherData_){ kind=1; otherData=otherData_; } };
Storage<Parent> mixed(10);
int main(){
srand(15454);
printf(" ==== initialize by random data and radom type \n");
for(int i=0; i<mixed.n; i++){
if ((rand()&4) > 0){
int someData = rand()&0xFF;
mixed.put(i, new ChildA( someData));
printf(" i %i ChildA.someData = %i \n", i , someData);
}else{
double otherData = (rand()&0xFF)/256.0f;
mixed.put(i, new ChildB( otherData));
printf(" i %i ChildB.otherData = %f \n", i , otherData);
};
};
printf(" ==== recall stored data \n");
for(int i=0; i<mixed.n; i++){
Parent* obj = mixed.get(i);
if (obj->kind ==0){ printf(" i %i ChildA.someData = %i \n", i , ((ChildA *) obj)->someData ); }
else { printf(" i %i ChildB.otherData = %f \n", i , ((ChildB *) obj)->otherData); };
};
}
早在您感興趣的問題的標題:「我想要做壞事」的領土。然後用**來說,「是的,真的很糟糕,沒有什麼能阻止我,你聽到了!」。好的,尋求幫助的不協調。 –
有更好的東西,例如['的std :: tuple'](http://en.cppreference.com/w/cpp/utility/tuple)。 –
乾杯和hth。 - Alf> probaly它只是缺乏我的知識。你可以說得更詳細點嗎。還有什麼我可以做動態可分配的指針數組比**? –