c++03 - How Do I Return a Templated Class in c++? -


i'm looking combine pointer data , type:

data_pointer.h

class datapointer {  public:   datapointer(t *data) : data_(data) {   }    inline double data() { return double(*data_); }   private:   t *data_; }; 

in there, data_ pointer data can of different type, user wants returned double. keep type , data , switch on type when returning in data() function, seems harder needs be.

the following example super contrived, think gets point across:

main.cpp

#include "data_pointer.h"  enum type {   kshort,   kint,   kfloat,   kdouble };  datapointer d() {   type t;   // char * returned, pointer other type indicated   // type parameter (void * standard way of doing this)--   // straight c library   char *value = getvaluefromexternalsource(&type);    switch (type) {   case kshort: return datapointer<short>(reinterpret_cast<short *>(value));   case kint: return datapointer<int>(reinterpret_cast<int *>(value));   case kfloat: return datapointer<float>(reinterpret_cast<float *>(value));   case kdouble: return datapointer<double>(reinterpret_cast<double *>(value));   } }  int main(int argc, char *argv[]) {   float f = 13.6;   asi::datapointer<float> dp(&f); // works fine    printf("%f\n", dp.data()); } 

compilation, though, gives me 'datapointer' not name type, @ declaration of d() makes sense because doesn't have type associated it.

i'm pretty sure i'm misunderstanding way templates supposed work, , i'm sure i'm missing syntactical knowledge, can guys this? i'm open different ways of doing this.

i'm running g++ 3.4.6, know wildly out of date, you're limited things outside control.

thanks.

you don't want using template here. if have value can 1 of multiple types want use union.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -