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
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
> 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...
> 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?
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
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...
> 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
> 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;
> }
> delete &b;
>
> > }