简体   繁体   中英

C++/CLI object casting

sort of new in CLI and wrapper making and got a question about pointers and object casting.

Header of my native code:

class Telegram : public Message
{
  public:
     Telegram(const Telegram& tele);
     int variablesX;
     int variablesY;
}

Source of the native code:

Telegram::Telegram(const Telegram& tele) : Message(tele.lenght)
{
   variablesX = tele.variablesX;
   variablesY = tele.variablesY;
}

And the trying to get into managed code: header:

   public ref class TelegramWrapper : public MessageWrapper
    {
       public:
        TelegramWrapper(eoTelegramWrapper^ tel):
       private:
        Telegram *myTelegram;
    }

source:

TelegramWrapper::TelegramWrapper(TelegramWrapper^ tel) : MessageWrapper(tel->lenght)
{
   myTelegram = new Telegram(reinterpreter_cast<Telegram%>(tel));
}

Can compile, no error just in the C# application cannot use because i receive the error: 'TelegramWrapper telegram' is not supported by the language. I have tried safe_cast too but because of the pointer differencies it is not possible. How could type casting the TelegramWrapper object into Telegram? Thanks for the answers in advance!

I'm not all familiar with C++/CLI, but if you're trying to make a wrapper then you should wrap instead of cast (unless I'm missing something obvious).

public ref class TelegramWrapper : public MessageWrapper
{
    public:
        TelegramWrapper(TelegramWrapper^ tel);
        TelegramWrapper(const &Telegram tel);
    private:
        Telegram *myTelegram;

    property int MyVariablesX {
        public: int get { return myTelegram->someVariablesX; }
        public: void set(int value) { myTelegram->someVariablesX = value; }
    }

    property int MyVariablesY {
        public: int get { return myTelegram->someVariablesY; }
        public: void set(int value) { myTelegram->someVariablesY = value; }
    }
};

TelegramWrapper::TelegramWrapper(TelegramWrapper^ tel) : MessageWrapper(tel->Lenght)
{
    myTelegram = tel->myTelegram;
}

TelegramWrapper::TelegramWrapper(const &Telegram tel) : MessageWrapper(tel.lenght)
{
    myTelegram = tel;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM