J
Jon Slaughter
Is there any way to get the class type from a pointer?
I'm working on a class that acts as a set:
I have two classes, one called FSet and the other called Element.
FSet inherets Element and has an internal pointer to a list of pointer of
Element type.
I have method in FSet that adds an Element to the list(does memory
management and checks if its already there).
So, I might I can do something like this:
FSet S;
FSet S1;
SomeClass C;
Element A, B, C;
S.Add(A);
S.Add(B);
S.Add(C);
S.Add((Element)S);
S.Add((Element)C);
Now, I would like to write a method that lets me get an element from the
list, but I would also need to know what class type it is.
i.e. I would need something like:
void *C = S.Get(3);
if (ClassType(C) == 'FSet')
{
..........
}
if (ClassType(C) == 'Element')
{
........
}
if (ClassType(C) == 'etc')
{
........
}
I'm not sure if its possible to do this or not ;/ (I'm sure its possible,
but not sure if C++ keeps track of class types and there instantiation(I
doubt it does)).
If what I'm trying to do is not possible the way I am doing it, is there
another way? I want to create a class that represents a set of "arbitrary
elements"(i.e. any class type, not just of one type).
Thanks
Jon
I'm working on a class that acts as a set:
I have two classes, one called FSet and the other called Element.
FSet inherets Element and has an internal pointer to a list of pointer of
Element type.
I have method in FSet that adds an Element to the list(does memory
management and checks if its already there).
So, I might I can do something like this:
FSet S;
FSet S1;
SomeClass C;
Element A, B, C;
S.Add(A);
S.Add(B);
S.Add(C);
S.Add((Element)S);
S.Add((Element)C);
Now, I would like to write a method that lets me get an element from the
list, but I would also need to know what class type it is.
i.e. I would need something like:
void *C = S.Get(3);
if (ClassType(C) == 'FSet')
{
..........
}
if (ClassType(C) == 'Element')
{
........
}
if (ClassType(C) == 'etc')
{
........
}
I'm not sure if its possible to do this or not ;/ (I'm sure its possible,
but not sure if C++ keeps track of class types and there instantiation(I
doubt it does)).
If what I'm trying to do is not possible the way I am doing it, is there
another way? I want to create a class that represents a set of "arbitrary
elements"(i.e. any class type, not just of one type).
Thanks
Jon