Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Type casting in C++\CLI project

Type casting in C++\CLI project

Scheduled Pinned Locked Moved Managed C++/CLI
questionc++dotnethelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    for study
    wrote on last edited by
    #1

    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?

    D 1 Reply Last reply
    0
    • F for study

      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?

      D Offline
      D Offline
      David Knechtges
      wrote on last edited by
      #2

      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[^]

      M L 2 Replies Last reply
      0
      • D David Knechtges

        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[^]

        M Offline
        M Offline
        Member 2595383
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • D David Knechtges

          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[^]

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          TNT

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups