Solved

a variable of type type

Posted on 1998-12-19
24
205 Views
Last Modified: 2010-04-02
is there a variable that holds a type, for instance:

type TypeVar1 = char;
type TypeVar2 = int;

TypeVar1 = TypeVar2;// both int

do you know a way this can be done?  
0
Comment
Question by:Booth882
  • 9
  • 5
  • 3
  • +6
24 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1180579
how about typecasting?!?!
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1180580
Perhaps you can use templates.
0
 
LVL 1

Author Comment

by:Booth882
ID: 1180581
also I want to be able to use it like:

   TypeVar1 AnInt = 88;

I've thought about templates but there are problems with it.  one is that templates must be declared like this:

   type<char> TypeVar1;

which would defeat the purpose of the whole thing.  I would only be able to have TypeVar1 hold char.  any ideas?
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1180582
a problem with templates is it doesn't hold the type - it's a different class for each type...i guess you could use RTTI, but I forsee a lot of if/else or switch/case if you try this...in the near future

perhaps create a variant class ?

something like

// what type
class Variant_Type
{
public:
enum ETYPE
{
E_CHAR,
E_BOOL,
E_INT,
E_DOUBLE,
// etc
} ;

ETYPE m_etype ; // what type holds

Variant_Type( eType ) { m_etype = eType ; }

void * NewType()
{
switch (m_etype)
{
case E_CHAR : return new char[1] ; break ;
case E_BOOLEAN : return new bool ; break ;
case E_INT : return new int ; break ;
// etc
}
} ;

class Variant
{
private:
Variant_Type m_type :
public:
// etc
} ;


Or if using MFC - they've made one for you - COleVariant
0
 
LVL 22

Expert Comment

by:nietod
ID: 1180583
That is not possible, but most likely there is a better way to achieve your ultimate goal.   (answer2000s solution is one, but given more information there is likely to be better solutions.)

So what is it you are ultimately trying to do?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1180584
>> do you know a way this can be done?
Not in C++.

Depending on what you want to accomplish you could use templates, RTTI and/or unions.

union types
{
    char char_type;
    unsigned char unsigned_char_type;
    short short_type;
    unsigned short unsigned_short_type;
    int int_type;
    unsigned int unsigned_int_type;
    long long_type;
    unsigned long unsigned_long_type;
    float float_type;
    unsigned float unsigned_float_type;
    double double_type;
    unsigned double unsigned_double_type;
    // etc.
};
0
 
LVL 1

Author Comment

by:Booth882
ID: 1180585
yeah I was hoping for something template like... but yeah it seemed to me there was no way to do this either.  

well then if I cant do that, can I have an array of template objects of a different type?  like:

   tem<int> IntTemplate;
   tem<char> CharTemplate;

   tem TemplateArray[] = {IntTemplate, CharTemplate};

is there a way to do this?  maybe with pointers??
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1180586
You can use a void * pointer, the problem is when you get the object you won't know if it's a char, int, etc. object

A way round this might to be make all our templates derive from a common non-template root class : but if you take this to extremes - this ends up being the same as a variant solution (but with more code)
0
 

Expert Comment

by:kelso
ID: 1180587
You can use RTTI

      int i;
      char ch;

      bool fA = (&typeid(ch) == &typeid(char)); // true
      bool fB = (&typeid(i) == &typeid(char)); // false

BTW, the type returned by the typeid operator is const type_info&
0
 
LVL 22

Expert Comment

by:nietod
ID: 1180588
How does that help?  

First of all typeid() only works dynamically with classes that have virtual functions.  But more importantly he doesn't need to know an object's type.  We wants to be able change the type assoicated with a storage location.  i.e. declate a variable that stores an int and later change it to store a char--or any other type.
0
 
LVL 1

Author Comment

by:Booth882
ID: 1180589
nietods right.  that doesnt help.  what type of information is a type such as 'char'?  is it all in the compiler or is it an actual type, such as kelso said 'const type_info &'.  is there a way I can use that?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1180590
The information returned by typeid() is useful for some things, but not really for a lot.  It is best used in a class hierarchy for determining what class you are actually working with when you have a base class pointer.  However, it has lots of limitations, the most important of which is that it is not dynamic, that is the information returned is not dependent on the actual object used at run-time but depends on the type defined at compile-time UNLESS the object is a class with at least one virtual function.  This it tends to be of no value with things like char and int.

Can you give us a better idea of what you are actually tryng to accomplish.?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:Booth882
ID: 1180591
alright heres what I'm trying to do.  I have a class that interfaces with lots of varied types of a single template class.  like:

   template<class T>
   class B{};

   class A
   {
   private:
      B<int> IntB;
      B<char> CharB;
      B<float> FloatB;
   };

the problem with that is I want it to create and destroy B objects dynamically, so it can grow or shrink.  and I dont want to be set with a predetermined type.  I may not use a certain type in one A object but in another I use three of them.  I thought of making a pointer to a B object for each type, but then I wouldnt be able to have multiple B objects of the same type.  also I would rather not load the class down with excess baggage.  I prefer to have only what is necessary in any class, with the opportunity to make room for things later on if they are needed.  

any ideas on how I could do this?  
0
 
LVL 5

Expert Comment

by:yonat
ID: 1180592
It looks as if you need to have all your B-s derived from a single base class, and have A contain a bag of B-s:

template<class T>
class B : public BBase
{
    // ...
};

class A
{
private:
    std::list<B*> itsBs; // you'd probably want some kind of map, so you can access specific B-s
public:
    AddB(B* b);
    // ...
};

Does that help? If this is totally off mark, maybe you can explain more about what A and B stand for.
0
 
LVL 5

Expert Comment

by:yonat
ID: 1180593
Oops, I meant:

class A
{
private:
    std::list<BBase*> itsBs;
public:
    AddB(BBase* b);
    // ...
};
0
 
LVL 1

Author Comment

by:Booth882
ID: 1180594
can I make BBase nontemplate and B a template???
0
 
LVL 5

Expert Comment

by:yonat
ID: 1180595
Yes, that's the idea. In fact, this is a common method to avoid "code bloat" in compilers that do not support separate template compilation: You put all "meaningful" code into a type-neutral base class (eg a list of void*), then derive a template from it, that does mostly type conversions (eg from void* to T*).
0
 
LVL 3

Expert Comment

by:altena
ID: 1180596
If you are programming windows, you may
want to take a look at the documentation of
the Variant type.

This more or less lets you do what you want,
but its not as simple as you want it to be.
0
 
LVL 1

Author Comment

by:Booth882
ID: 1180597
altena this was already suggested and dismissed.  thank you though for your help.
0
 
LVL 1

Author Comment

by:Booth882
ID: 1180598
yonat - can you put this idea into my generic A and B classes so that I can see its syntax?  feel free to add classes, like BBase or something.  I like the idea I just want to see how it would be done.  then the points are yours.  
0
 
LVL 5

Expert Comment

by:yonat
ID: 1180599
OK, here is some code that works on VC6. It is just an illsutration - you probably have different stuff to put insode your B-s.

#include <list>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>

class BBase
{
public:
    virtual std::string Value() const = 0; // just an illustration
    // whatever else all B-s has in common as pure abstract functions
    virtual ~BBase() {}
};

template <class T>
class B : public BBase
{
private:
    T itsT; // just an illustration
public:
    B(T t = T()) : itsT(t) {}
    virtual std::string Value() const // just an illustration
    {
        std::stringstream s;
        s << itsT;
        return s.str();
    }

    // ...
};

// small utility class I use - just for the illustrations
template <class Ptr>
class Deleter
{
public:
    void operator()(Ptr p) {delete p;}
};

// another small utility function for illustration
inline void print_B(const BBase* b)
{
    std::cout << b->Value() << std::endl;
}

class A
{
private:
    std::list<BBase*> itsBs;
public:
    void AddB(BBase* b)
    {
        itsBs.push_back(b);
    }

    void PrintAll() // print all itsBs
    {
        std::for_each(itsBs.begin(), itsBs.end(), print_B);
    }

    ~A() // delete all itsBs
    {
        std::for_each(itsBs.begin(), itsBs.end(), Deleter<BBase*>());
    }

    // ...
};

int main()
{
    A a;
    a.AddB(new B<int>(42));
    a.AddB(new B<char>('x'));
    a.AddB(new B<std::string>("Zaphod"));
    a.PrintAll();

    return 0;
}

0
 
LVL 1

Author Comment

by:Booth882
ID: 1180600
ah!  beautiful!! :)  that is exactly what I was looking for, thank you yonat.  feel free to answer this question.  
0
 
LVL 5

Accepted Solution

by:
yonat earned 100 total points
ID: 1180601
Good luck!
0
 
LVL 1

Author Comment

by:Booth882
ID: 1180602
thanks yonat!!!! :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now