• Advertisement
Sign in to follow this  

Is it Possible to Return a Variables Type?[resolved]

This topic is 4502 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, I was hoping I wouldn't have to ask, but I do. I want to be able to return the type that a variable is, kinda confusing, so I'll show you what I mean.
typedef struct {
   int x;
   bool y;
} i;

typedef struct {
   float x;
   double y;
} f;

template <class T> 
class tempclass
{
    public:
        T a;
        T b; //For variable b, I want it to be whatever type the y section is for the typedefs
             //So if it was templcass<i>, I'd want it to be bool b.  But if it was tempclass<f>
             //I'd want it to be double b.
} ;
If you know of anyway to do this, I'd appreciate knowing. Thanks, --Michael [Edited by - MikeTacular on December 29, 2005 12:57:27 AM]

Share this post


Link to post
Share on other sites
Advertisement
You're probably looking for something like this:

struct i {
int x;
typedef bool Type;
Type y;
};

struct f {
float x;
typedef double Type;
Type y;
};

template<class T>
struct tempclass {
T a;
typename T::Type b;
};


Also, typedef struct isn't really needed in C++.

Share this post


Link to post
Share on other sites
Hey thanks a ton. It works perfect now. Also, I never could get it to work without doing typedef struct, but from looking at your code, I just kept forgetting to delcare its name. Thanks.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement