C++/CLI : Advantages over C#

前端 未结 11 2225
自闭症患者
自闭症患者 2021-01-05 00:54

Is there any major advantage of managed C++/CLI over C#. Definitely not the syntax I suppose as the following code in C++/CLI is real ugly,

C++/CLI code:

<         


        
11条回答
  •  礼貌的吻别
    2021-01-05 01:18

    Being able to use native headers files directly is a huge advantage, but not the only one.

    Stack semantics are so much better than anything C# has to offer for IDisposable management. C++/CLI has one uniform syntax for correctly managing variables which are IDisposable and those which aren't, both as local variables and as member fields. Comparison:

    ref class MyClass
    {
       FileStream fs;
    }
    

    vs

    class MyClass : IDisposable
    {
      FileStream fs;
    
      void IDisposable.Dispose() { Dispose(true); }
      ~MyClass() { Dispose(false); }
    
      public virtual void Dispose(bool disposing) { if (disposing) fs.Dispose(); }
    }
    

    Now which language is looking ugly?

    Then there are templates, interior_ptr, #define, native DLL exports, pointer-to-member, and probably several other things I've forgotten.

提交回复
热议问题