Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

typeid question

2 views
Skip to first unread message

Tom Plunket

unread,
Apr 13, 2001, 9:48:09 PM4/13/01
to
Can I instantiate an object somehow from a typeid?

Is the following a possible way to do it?

void fn(void* thing)
{
const type_info& ti = typeid(thing);

ti->type anotherObjectOfSameType;
}

? Looks wrong and I'll bet it is. Just curious.

-tom!

--
Tom Plunket to...@fancy.org
PlayStation2/3D Studio geek
"Our music is simple, it's totally fake. It's done by
machines 'cause they don't make mistakes." -KMFDM

Neil Butterworth

unread,
Apr 13, 2001, 9:01:59 PM4/13/01
to
"Tom Plunket" <to...@fancy.org> wrote in message
news:nsafdtklmognhrrpu...@4ax.com...

> Can I instantiate an object somehow from a typeid?
>
> Is the following a possible way to do it?
>
> void fn(void* thing)
> {
> const type_info& ti = typeid(thing);
>
> ti->type anotherObjectOfSameType;
> }
>
> ? Looks wrong and I'll bet it is. Just curious.
>

No - your code won't compile, and C++ has no built-in persistence mechanism,
which is what you are asking for. You can get persistence (though not using
typeid directly) via a number of object-oriented database products. For
example:

ObjectStore www.odi.com
Poet www.poet.com


NeilB


Tom Plunket

unread,
Apr 13, 2001, 10:04:56 PM4/13/01
to
Neil Butterworth wrote:

> No - your code won't compile, and C++ has no built-in persistence mechanism,
> which is what you are asking for. You can get persistence (though not using
> typeid directly) via a number of object-oriented database products. For
> example:
>
> ObjectStore www.odi.com
> Poet www.poet.com

Thanks. I was looking for something a little more straight-
forward (e.g. a function call) since this is going into what is
going to be a very small application that directly loads (small)
files off of disk...

Guess I'm going to be brewing tonight. ;)

-tom!

--
Tom Plunket to...@fancy.org
PlayStation2/3D Studio geek

What do you mean, it doesn't work? It worked fine yesterday...

Tom Plunket

unread,
Apr 14, 2001, 12:01:35 AM4/14/01
to
Tom Plunket wrote:

> Can I instantiate an object somehow from a typeid?

I just thought of a better example for what I'm looking for.

Say I load up a map with typeids, and I get a typeid out of this
map by indexing with my own enum indicating exactly what this
type is.

typedef std::map<MyEnum, const type_info&> MyMap;
typedef MyMap::value_type MapFiller;
const MapFiller mapFillerThing[] =
{
MapFiller(Integer, typeid(int)),
MapFiller(Float, typeid(float)),
// etc.
};

MyMap myMap(&mapFillerThing[0], &mapFillerThing[2]);


ok, so now I want to be able to "build" something from this:

typedef std::vector<MyEnum> EnumList;
void func(EnumList enumList)
{
EnumList::const_iterator curEnum;

for (curEnum = enumList.begin() ;
curEnum != enumList.end() ;
++curEnum)
{
// sorry don't recall exactly proper syntax
// if the below line is "bad"
std::vector<myMap[*curEnum]> aVector;

// do stuff with aVector
}
}

Does this make any sense, even if it is totally undoable?

Thanks-

-tom!

--
Tom Plunket to...@fancy.org
PlayStation2/3D Studio geek

You seem familiar. Have you been in Halas lately?

Sai Shankar

unread,
Apr 14, 2001, 1:55:18 AM4/14/01
to
Hi,

If you want all this to happen at compile time, look up
"Andrei Alexandrescu" and "typelist" in google.
He provides functions for iterating over lists of types.

At run-time, you might want to look into the "Prototype"
Pattern from Design Patterns by Eric Gamma et al.

> > Can I instantiate an object somehow from a typeid?
>
> I just thought of a better example for what I'm looking for.
>
> Say I load up a map with typeids, and I get a typeid out of this
> map by indexing with my own enum indicating exactly what this
> type is.
>

[snip]

>
> ok, so now I want to be able to "build" something from this:
>
> typedef std::vector<MyEnum> EnumList;
> void func(EnumList enumList)
> {
> EnumList::const_iterator curEnum;
>
> for (curEnum = enumList.begin() ;
> curEnum != enumList.end() ;
> ++curEnum)
> {

[snip]

> }
> }
>
> Does this make any sense, even if it is totally undoable?

Probably not :-), but i havent had my coffee yet.

Regards
Sai

David Hilsee

unread,
Apr 15, 2001, 5:15:17 PM4/15/01
to
Maybe you can create a template function to do what you're trying to do.
It's hard to see what the purpose of this function is from your description.

Or maybe you need a cloning method for the passed object.

David Hilsee

"Tom Plunket" <to...@fancy.org> wrote in message
news:nsafdtklmognhrrpu...@4ax.com...

SanPedro

unread,
May 4, 2001, 7:38:30 PM5/4/01
to

> Tom Plunket wrote:
>
> > Can I instantiate an object somehow from a typeid?
>
> I just thought of a better example for what I'm looking for.
>
> Say I load up a map with typeids, and I get a typeid out of this
> map by indexing with my own enum indicating exactly what this
> type is.

You're on the right track, but here's what you need to do...

Load a map with either "typeid"s (or unique "string"s that define the
class) and an instance of that class. Now here's the trick...all classes
that you want to do this with must derive from the same base class, and
must implement some function (say Clone() ) that creates a new object of
this type.

e.g.

class BaseClass;
typedef std::map<std::string, const BaseClass&> MyMap;

class BaseClass
{
public:
virtual BaseClass& Clone()
{
return *(new BaseClass);
}
};

class Derived1 : public BaseClass
{
public:
Derived1& Clone()
{
return *(new Derived1);
}
};

class Derived2 : public BaseClass
{
public:
Derived2& Clone()
{
return *(new Derived2);
}
};

int main()
{
Derived1 d1;
Derived2 d2;

MyMap map;
map["der1"] = d1;
map["der2"] = d2;

BaseClass& b = map["der2"].Clone();
}


The map is really only useful if you're reading the strings/classes from a
file (the string tells you which type of class/object you need to
create). If the objects were already in memory, then you could just call
Clone() on the object you're holding instead of looking up the typeid (or
enum) in a map.

Hope this helps...
Pedro

SanPedro

unread,
May 4, 2001, 7:44:53 PM5/4/01
to
Oops, don't forget to delete the allocated memory. I'd hate to think I'm
advocating bad practices. :o)

> int main()
> {
> Derived1 d1;
> Derived2 d2;
>
> MyMap map;
> map["der1"] = d1;
> map["der2"] = d2;
>
> BaseClass& b = map["der2"].Clone();

// Originally forgot this next line

delete *b;

> }

SanPedro

unread,
May 9, 2001, 4:00:26 PM5/9/01
to
Correction below...

> delete &b;
>
> > }

0 new messages