Type casting in C++\CLI project
-
I have project which I am compiling with /clr. I have a class like below..
ref class A
{
public:
void CheckValue(void * test);
typedef ref struct val
{
std::string *x;
}val_t;};
in my implementation I ahve to use something like below..
void A::CheckValue(void *test)
{
a::val_t^ allVal = (a::val_t^)test;
}in my main I have used like..
int main()
{
A^ obj = gcnew A();
a::val_t valObj = new std::string("Test");
obj->CheckValue((void*)valObj);
}I am getting type cast error and two places - obj->CheckValue((void*)valObj); and at obj->CheckValue((void*)valObj); error C2440: 'type cast' : cannot convert from 'void*' to 'A::val_t ^' This snippet is just to show behavior at my end and I ahve to use it this way only. Earlier I was running it using non /clr so it compiled fine. Now question I have how can I make this type casting work in C++/CLI type project?
-
I have project which I am compiling with /clr. I have a class like below..
ref class A
{
public:
void CheckValue(void * test);
typedef ref struct val
{
std::string *x;
}val_t;};
in my implementation I ahve to use something like below..
void A::CheckValue(void *test)
{
a::val_t^ allVal = (a::val_t^)test;
}in my main I have used like..
int main()
{
A^ obj = gcnew A();
a::val_t valObj = new std::string("Test");
obj->CheckValue((void*)valObj);
}I am getting type cast error and two places - obj->CheckValue((void*)valObj); and at obj->CheckValue((void*)valObj); error C2440: 'type cast' : cannot convert from 'void*' to 'A::val_t ^' This snippet is just to show behavior at my end and I ahve to use it this way only. Earlier I was running it using non /clr so it compiled fine. Now question I have how can I make this type casting work in C++/CLI type project?
You are mixing the two - managed and unmanaged - void * is unmanaged and val_t is managed. You need to do a conversion in order to get this to work. Here is a similar question: http://stackoverflow.com/questions/6903208/cli-c-void-to-systemobject[^]
-
You are mixing the two - managed and unmanaged - void * is unmanaged and val_t is managed. You need to do a conversion in order to get this to work. Here is a similar question: http://stackoverflow.com/questions/6903208/cli-c-void-to-systemobject[^]
As we know C++ disdains poor typing. Some day it will achieve Pascal or Ada greatness! Try: desired_ptrtype newone=static_cast(voidstarptr) Daniel Kilsdonk
-
You are mixing the two - managed and unmanaged - void * is unmanaged and val_t is managed. You need to do a conversion in order to get this to work. Here is a similar question: http://stackoverflow.com/questions/6903208/cli-c-void-to-systemobject[^]