473,698 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

distinguishing wheter void* ptr points to class A or class B?


Hello!

I have some legacy C code which expects a pointer to a function to
be evaluated at one point.
Because of the pointer-to-function vs. pointer-to-member
incompatibility , this needs to be a
global function. To aid the passing of some extra data to the
function, it takes an extra
parameter of type void* which is passed uninterpreted to it.

I am in a situation where this void* pointer can point either to
class A or to a class B,
which are not related. Is there a way to perform a reliable cast in
the function or otherwise
distinguish which the void* pointer actually points to? This is
compiled as C++.
I can static_cast<to whatever, but, obviously, if I get the class
wrong, this segfaults
at the first dereference of a member. dynamic_cast<do es not work on
void*.

Is there a way out? I can modify classes A and B at will and the
function in question,
but its signature must remain intact.

TIA,
- J.

Sep 22 '07 #1
6 1872
I have some legacy C code which expects a pointer to a function to
be evaluated at one point.
Because of the pointer-to-function vs. pointer-to-member
incompatibility , this needs to be a
global function. To aid the passing of some extra data to the
function, it takes an extra
parameter of type void* which is passed uninterpreted to it.

I am in a situation where this void* pointer can point either to
class A or to a class B,
which are not related. Is there a way to perform a reliable cast in
the function or otherwise
distinguish which the void* pointer actually points to? This is
compiled as C++.
I can static_cast<to whatever, but, obviously, if I get the class
wrong, this segfaults
at the first dereference of a member. dynamic_cast<do es not work on
void*.

Is there a way out? I can modify classes A and B at will and the
function in question,
but its signature must remain intact.
I'm not really sure whether this is valid, but I would try to define
some class C as:

class C
{
public:
virtual int getType() =0;
};

And have A and B inherit from C; then, cast your void* to C* (this is
what I'm not sure about whether it is valid to do), query for the type,
and proceed accordingly.

cheers,
Daniel
--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!
Sep 22 '07 #2
Daniel Kraft wrote:
class C
{
public:
virtual int getType() =0;
};

And have A and B inherit from C; then, cast your void* to C* (this is
what I'm not sure about whether it is valid to do), query for the type,
and proceed accordingly.
Once you have a common polymorphic ancestor you can also use
dynamic_cast or typeid.
Sep 22 '07 #3
On Sep 22, 6:27 am, jacek.dzied...@ gmail.com wrote:
I have some legacy C code which expects a pointer to a function to
be evaluated at one point.
Because of the pointer-to-function vs. pointer-to-member
incompatibility , this needs to be a
global function. To aid the passing of some extra data to the
function, it takes an extra
parameter of type void* which is passed uninterpreted to it.

I am in a situation where this void* pointer can point either to
class A or to a class B,
which are not related. Is there a way to perform a reliable cast in
the function or otherwise
distinguish which the void* pointer actually points to? This is
compiled as C++.
I can static_cast<to whatever, but, obviously, if I get the class
wrong, this segfaults
at the first dereference of a member. dynamic_cast<do es not work on
void*.
You could simple define a small structure with a type tag and a
pointer:

struct C {
int type;
void *ptr; // to either an object of class A or class B
}

When you create the structure, modify the type field accordingly so
that it can be read back once you have to determine what class the
objects belong to. After that, it's just a simple static_cast<of the
'ptr' member.

Sep 22 '07 #4
On Sep 22, 11:48 am, "Alf P. Steinbach" <al...@start.no wrote:
* Justin.SpahrSum m...@gmail.com:
On Sep 22, 6:27 am, jacek.dzied...@ gmail.com wrote:
I have some legacy C code which expects a pointer to a function to
be evaluated at one point.
Because of the pointer-to-function vs. pointer-to-member
incompatibility , this needs to be a
global function. To aid the passing of some extra data to the
function, it takes an extra
parameter of type void* which is passed uninterpreted to it.
I am in a situation where this void* pointer can point either to
class A or to a class B,
which are not related. Is there a way to perform a reliable cast in
the function or otherwise
distinguish which the void* pointer actually points to? This is
compiled as C++.
I can static_cast<to whatever, but, obviously, if I get the class
wrong, this segfaults
at the first dereference of a member. dynamic_cast<do es not work on
void*.
You could simple define a small structure with a type tag and a
pointer:
struct C {
int type;
void *ptr; // to either an object of class A or class B
}
When you create the structure, modify the type field accordingly so
that it can be read back once you have to determine what class the
objects belong to. After that, it's just a simple static_cast<of the
'ptr' member.

Uhm, well.

In C++ it's usually ungood to explicitly discriminate on type, when a
virtual function can do that type-discrimination.

Here's one way to Do Things (off the cuff, not compiled):

typedef void (*CCallbackFunc )( void* );
typedef void (*CFuncThatCall sBack)( CCallbackFunc, void* );
void generalCallback ( void* );

struct AbstractCallbac kHandler
{
virtual void onCallback() = 0;
void* pointer() { return this; }
invoke( CFuncThatCallsB ack f ) { f( generalCallback , pointer(); }
Seems like you forgot a paren here.
};

CCallBackFunc generalCallBack ( void* arg )
I think you meant "void", not "CCallBackFunc" .
{
static_cast<Abs tractCallbackHa ndler*>( arg )->onCallback() ;
}

template< class T void callbackFor( T& o );

template< class T >
struct CallbackHandler
{
T* p;
CallbackHandler ( T& o ): p( &o ) {}
virtual void onCallback() { callbackFor( *p ); }
}

// ... code specific to using classes A and B:

template<callba ckFor<A>( A& aha ) { ... }
template<callba ckFor<B>( B& boo ) { ... }

extern "C" void someCFunc( CCallBackFunc callBackFunc, void* arg );

struct A {};
struct B {};

void doThingsTo( A& a )
{
CallbackHandler <A>( a ).invoke( someCFunc );
}
All of the code you gave took me a hell of a long time to parse... I
personally would opt for the simple and transparent structure. That
said, I couldn't have come up with the code you gave in a million
years... it's very clever.

Sep 22 '07 #5
Ron Natalie wrote:
Daniel Kraft wrote:
>class C
{
public:
virtual int getType() =0;
};

And have A and B inherit from C; then, cast your void* to C* (this is
what I'm not sure about whether it is valid to do), query for the type,
and proceed accordingly.
Once you have a common polymorphic ancestor you can also use
dynamic_cast or typeid.
You could use dynamic_cast on A* and B*. But you cannot use dynamic_cast to
get from void* to C*.
Best

Kai-Uwe Bux
Sep 22 '07 #6
"Daniel Kraft" <d@domob.euschr ieb im Newsbeitrag
news:fd******** **@newsreader2. utanet.at...
> I have some legacy C code which expects a pointer to a function to
be evaluated at one point.
Because of the pointer-to-function vs. pointer-to-member
incompatibilit y, this needs to be a
global function. To aid the passing of some extra data to the
function, it takes an extra
parameter of type void* which is passed uninterpreted to it.

I am in a situation where this void* pointer can point either to
class A or to a class B,
which are not related. Is there a way to perform a reliable cast in
the function or otherwise
distinguish which the void* pointer actually points to? This is
compiled as C++.
I can static_cast<to whatever, but, obviously, if I get the class
wrong, this segfaults
at the first dereference of a member. dynamic_cast<do es not work on
void*.

Is there a way out? I can modify classes A and B at will and the
function in question,
but its signature must remain intact.

I'm not really sure whether this is valid, but I would try to define some
class C as:

class C
{
public:
virtual int getType() =0;
};

And have A and B inherit from C; then, cast your void* to C* (this is what
I'm not sure about whether it is valid to do), query for the type, and
proceed accordingly.
In practice it probably works, but the original pointer, either to an
instance of A or B, must be cast to C* before the result of this cast is
converted to void*. Otherwise problems might occure when multiple
inheritance is used.

Heinz
Sep 23 '07 #7

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

Similar topics

2
3132
by: Xavier Decoret | last post by:
This is a compiler error that I get when I try to do: void* p; unsigned int stride; // ... p += stride; I have good reason to do this, namely an iterator over an array of object whose type may vary, as outlined below. So I don't understand why
27
2664
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
5
2369
by: trying_to_learn | last post by:
Hello All Have another qn from the practice exercises. I ve done the exercise and the birds fly member fn was called successfully. But i still cant answer the authors qn. why this ability to assign void * has had to be avoided in C++. I didnt even instantiate bird obj then how was i able to call a bird member fn. Im confused. Quote: "Qn)Create a class called bird that can fly( ) and a class rock that can’t. Create a rock object, take...
16
2433
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new CImplementation(); pInterface->Method(); Case 2:
6
6065
by: Pavils Jurjans | last post by:
Hello, Here's the sample XML: <sample1></sample1> <sample2/> From many XML books and online documentations, it is said to be just different syntax for the same data. However, when we analyze the node
56
3327
by: maadhuu | last post by:
hello, this is a piece of code ,which is giving an error. #include<stdio.h> int main() { int a =10; void *p = &a; printf("%d ", *p ); //error....why should it //be an error ?can't the compiler make out because //of %d that the pointer is supposed to refer to an integer ?? or is explicit type casting required ??
3
14492
by: vijay.gandhi | last post by:
Hi, I am trying to convert some unmanaged code (C++) to managed code (using C++/CLI). 1) One of the functions used returns a void* which I need to cast into a handle of a managed object. Can somebody please tell me how. Actually, the function that returns a void* is a part of a MFC class - CPtrList, used in my unmanaged code. I was searching of its equivalent
13
1478
by: Jay Dee | last post by:
Hi all, I am slightly confused about an odd result that douse not create an error, but also douse not give the result I would expect. My aim was to output data to a text file. If the data was passed by value, output the name and the data. If the data was by ref to output just the name as reference to the
26
1900
by: tjhnson | last post by:
Hi, With properties, attributes and methods seem very similar. I was wondering what techniques people use to give clues to end users as to which 'things' are methods and which are attributes. With ipython, I use tab completion all the time, but I can rarely tell from the names alone whether it is an attribute or method. Tips? Ideas? Best practices?
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.