G
Guest
Hello,
I have some code where I need to store a value as LPVOID. I'd like to be
able to do some runtime testing about whether the thing that's pointed to is
of a certain type. I've tried using RTTI's typeid and dynamic_cast functions
(the latter won't compile), but they've not worked for me. I've included a
short sample program that sort of shows what I'm trying to do and possibly
reveals whether I'm doing something dumb.
The output of the sample program is "I don't know what it is" repeated twice.
Any help would be appreciated,
Thanks,
Notre
#include "stdafx.h"
#include <typeinfo>
#include <stdio.h>
class A
{
public:
A() {m_a = 0;}
void SetA(int a)
{
m_a = a;
}
int GetA()
{
return m_a;
}
private:
int m_a;
};
class B
{
public:
B() {m_b = 0;}
void SetB(int b)
{
m_b = b;
}
int GetB()
{
return m_b;
}
private:
int m_b;
};
void TestTypeId(void* lpThing)
{
if (lpThing != NULL)
{
if (typeid(lpThing) == typeid(A))
printf("It's an A\n");
else if (typeid(lpThing) == typeid(B))
printf("It's a B\n");
else
printf("I don't know what it is.\n");
}
}
void TestDynamicCast(void* lpThing)
{
if (lpThing != NULL)
{
//A* pa = dynamic_cast<A*> (lpThing); //won't compile
//B* pb = dynamic_cast<B*> (lpThing); //won't compile
/*
if (pa != NULL)
printf("It's an A\n");
else if (pb != NULL)
printf("It's a B\n");
else
printf("I don't know what it is.\n");
*/
}
}
int _tmain(int argc, _TCHAR* argv[])
{
A a;
B b;
a.SetA(34);
b.SetB(45);
TestTypeId((void*) &a);
TestTypeId((void*) &b);
TestDynamicCast((void*) &a);
TestDynamicCast((void*) &b);
return 0;
}
I have some code where I need to store a value as LPVOID. I'd like to be
able to do some runtime testing about whether the thing that's pointed to is
of a certain type. I've tried using RTTI's typeid and dynamic_cast functions
(the latter won't compile), but they've not worked for me. I've included a
short sample program that sort of shows what I'm trying to do and possibly
reveals whether I'm doing something dumb.
The output of the sample program is "I don't know what it is" repeated twice.
Any help would be appreciated,
Thanks,
Notre
#include "stdafx.h"
#include <typeinfo>
#include <stdio.h>
class A
{
public:
A() {m_a = 0;}
void SetA(int a)
{
m_a = a;
}
int GetA()
{
return m_a;
}
private:
int m_a;
};
class B
{
public:
B() {m_b = 0;}
void SetB(int b)
{
m_b = b;
}
int GetB()
{
return m_b;
}
private:
int m_b;
};
void TestTypeId(void* lpThing)
{
if (lpThing != NULL)
{
if (typeid(lpThing) == typeid(A))
printf("It's an A\n");
else if (typeid(lpThing) == typeid(B))
printf("It's a B\n");
else
printf("I don't know what it is.\n");
}
}
void TestDynamicCast(void* lpThing)
{
if (lpThing != NULL)
{
//A* pa = dynamic_cast<A*> (lpThing); //won't compile
//B* pb = dynamic_cast<B*> (lpThing); //won't compile
/*
if (pa != NULL)
printf("It's an A\n");
else if (pb != NULL)
printf("It's a B\n");
else
printf("I don't know what it is.\n");
*/
}
}
int _tmain(int argc, _TCHAR* argv[])
{
A a;
B b;
a.SetA(34);
b.SetB(45);
TestTypeId((void*) &a);
TestTypeId((void*) &b);
TestDynamicCast((void*) &a);
TestDynamicCast((void*) &b);
return 0;
}