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.
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.