473,321 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Testing type of void*

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;
}
Apr 3 '06 #1
4 1138
On Mon, 3 Apr 2006 13:54:03 -0700, Notre Poubelle
<no************@online.nospam> wrote:
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.


There's no way to recover the type from a void*.

--
Doug Harrison
Visual C++ MVP
Apr 3 '06 #2
Doug Harrison [MVP] wrote:
On Mon, 3 Apr 2006 13:54:03 -0700, Notre Poubelle
<no************@online.nospam> wrote:
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.


There's no way to recover the type from a void*.

What the reason behind it..???
Apr 5 '06 #3
On Wed, 05 Apr 2006 18:49:04 +0530, Laxman <la*********@solversa.com>
wrote:
There's no way to recover the type from a void*.

What the reason behind it..???


The void* type can hold a pointer of any data type, and even if the data
contains some sort of self-identifying tag, it could be a spurious
occurrence of that tag. So the only reliable way to tell what a void*
points to is to remember it yourself. Things are much different in .NET
where everything derives from System.Object, and you can recover type
information when you have an Object reference.

--
Doug Harrison
Visual C++ MVP
Apr 5 '06 #4
Laxman wrote:
Doug Harrison [MVP] wrote:
On Mon, 3 Apr 2006 13:54:03 -0700, Notre Poubelle
<no************@online.nospam> wrote:
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.

There's no way to recover the type from a void*.

What the reason behind it..???


For a start, void* doesn't haven't to point to any object. e.g. it could
point to the 3rd byte of a double variable or to the memory location
immediately after an object. e.g.

int i;
void* p = &i + 1; //p doesn't point to any object!

Finally, C++ is an efficient language, and doesn't want to force every
object (even ints and doubles!) to carry around runtime information
about its type - if you want it, you should use your own base class with
virtual functions, or pass around some kind of tag with the void*
pointer to say what it is (a technique commonly used in C).

Tom
Apr 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Brian | last post by:
Hello all.. Am working on an Air Hockey game... have an table loaded into a picture box. The borders of the table are slightly slanted. Am using hit testing lines with GDI+ to manipulate the...
0
by: Matt | last post by:
Hi All. I have been trying to implement this front controller On MS site and finally i have no error on the pages but i dont its working or not. When i execute the pages from Browser i dont get no...
4
by: a | last post by:
I'm having trouble testing a custom object. I've tried many different approaches. One is shown below. The XML below shows the state of the object and I'm trying to test for that state, ie there...
2
by: Luc The Perverse | last post by:
Hello! I am trying to find a way to deal with Windows' case insensitivity without just forcing everything lowercase. While I am open to criticism on my method - my question relates to the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.