tags:

views:

65

answers:

2

Hi all,

I had a problem while hacking a bigger project so I made a simpel test case. If I'm not omitting something, my test code works fine, but maybe it works accidentally so I wanted to show it to you and ask if there are any pitfalls in this approach.

I have an OutObj which has a member variable (pointer) InObj. InObj has a member function. I send the address of this member variable object (InObj) to a callback function as void*. The type of this object never changes so inside the callback I recast to its original type and call the aFunc member function in it. In this exampel it works as expected, but in the project I'm working on it doesn't. So I might be omitting something or maybe there is a pitfall here and this works accidentally. Any comments? Thanks a lot in advance.

(The problem I have in my original code is that InObj.data is garbage).

#include <stdio.h>

class InObj
{
public:
 int data;
 InObj(int argData);
 void aFunc()
 {
  printf("Inside aFunc! data is: %d\n", data);
 };
};

InObj::InObj(int argData)
{
 data = argData;
}

class OutObj
{
public:
 InObj* objPtr;
 OutObj(int data);
 ~OutObj();
};

OutObj::OutObj(int data)
{
 objPtr = new InObj(data);
}

OutObj::~OutObj()
{
 delete objPtr;
}

void callback(void* context)
{
 ((InObj*)context)->aFunc();
}

int main ()
{
 OutObj a(42);
 callback((void*)a.objPtr);
}
+3  A: 

Yes, this is safe.

A pointer to any type can be converted to a pointer to void and back again.

Note that the conversion to void* is implicit, so you don't need the cast.

James McNellis
All pointers are the same size and contain the same data right? The types are just to remind you.
Brendan Long
@Brendan Long: That's not necessarily true. If you have multiple inheritance hierarchies, a pointer cast may involve actually modifying the pointer to point to the correct vtbl structure.
Billy ONeal
@Brendan: There are a lot of intricacies with pointers. Pointers to members may not have the same size as pointers to objects. One of guaranteed "will work" things is that if you have a pointer to some object of type `T`, you can convert a `T*` to a `void*` and then back to a `T*` and the original `T*` and the resulting `T*` will point to the same object. That's the rule that's really important for this question.
James McNellis
Interesting. I had no idea it was so complicated.
Brendan Long
@Brendan: That's the best part of C++: even the "simple" things are complicated :-D
James McNellis
A: 

What you have posted should be "safe" insofar as a non type safe operation like this can be safe. I would replace the casts you have though with static_cast instead of C style casts, because static_cast doesn't allow you to make such unsafe conversions between types. If you try to do something unsafe with static_cast the compiler will tell you instead of leaving you guessing.

(Side unrelated note: InObj's constructor should use initialization rather than assignment:

InObj::InObj(int argData) : data(argData)
{
}

)

Billy ONeal