About void* usage in CLI/C++

E

Ed

Hi, guys,
I met a very strange problem while using void* in CLI\C++.
Code like this:

CLI\C++ Code:

static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseClass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();

//Assign native pointer to a void* pointer.
void* voidPtr = native;

//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
static_cast<NativeNameSpace::ChildClass*>(voidPtr );
//NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)voidPtr ;

//It's failded !!!!!!!
child->AddRef();

return gcnew ChildClass(child );
}

int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}


If I write the code like following. I don't use void* anymore, and
it's OK.


static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseClass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();

//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)(native);

//It's OK!!!!!!!
child->AddRef();

return gcnew ChildClass(child );
}

int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}



Obviously, the void* usage break the code down. Is there any taboo
while using void* in CLI\C++?


Thanks,
Ed.
 
B

Ben Voigt [C++ MVP]

Ed said:
Hi, guys,
I met a very strange problem while using void* in CLI\C++.
Code like this:

CLI\C++ Code:

static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseClass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();

//Assign native pointer to a void* pointer.
void* voidPtr = native;

//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
static_cast<NativeNameSpace::ChildClass*>(voidPtr );
//NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)voidPtr ;

//It's failded !!!!!!!
child->AddRef();

return gcnew ChildClass(child );
}

int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}


If I write the code like following. I don't use void* anymore, and
it's OK.


static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseClass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();

//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)(native);

//It's OK!!!!!!!
child->AddRef();

return gcnew ChildClass(child );
}

int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}



Obviously, the void* usage break the code down. Is there any taboo
while using void* in CLI\C++?

I think you ought to use a dynamic_cast, otherwise the void* might be
getting the address of the base sub-object, not the full object, and that
would cause the static_cast to do the wrong thing.
 
E

Ed

I think you ought to use a dynamic_cast, otherwise the void* might be
getting the address of the base sub-object, not the full object, and that
would cause the static_cast to do the wrong thing.

//Assign native pointer to a void* pointer.
void* voidPtr = native;

NativeNameSpace::ChildClass* child =
dynamic_cast<NativeNameSpace::ChildClass*>(voidPtr );

Right?
 
E

Ed

//Assign native pointer to a void* pointer.
void* voidPtr = native;

NativeNameSpace::ChildClass* child =
dynamic_cast<NativeNameSpace::ChildClass*>(voidPtr );

Right?

I know the reason. If ChildClass is multiple inheritation, the
static_cast from void* to ChildClass is incorrect.
So, do not use void* as much as possible.
 
E

Ed

//Assign native pointer to a void* pointer.
void* voidPtr = native;

NativeNameSpace::ChildClass* child =
dynamic_cast<NativeNameSpace::ChildClass*>(voidPtr );

Right?

I know the reason. If ChildClass is multiple inheritance, the
static_cast from void* to ChildClass is wrong.
So do not use void* as much as possible in C++ code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top