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. How to use C++ ATL-Template class in c#

How to use C++ ATL-Template class in c#

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++questiontutorial
2 Posts 2 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.
  • C Offline
    C Offline
    Chiman1
    wrote on last edited by
    #1

    I am migrating a C++ application to .NET. In C++ ,I have an template class which uses some ATL library classes for eg. template< class T > class MyBaseClass: public IConnectionPointContainerImpl, public IPersistStorageImpl, public IPersistPropertyBagImpl, { } Now i need to use this call in my new C# .net project where my new .NET call will get derive from above MyBaseClass . Please let me know how can i archive this ??? Is this possible to use a template class which uses some ATL libraray classes directly in C# ? `chiman

    N 1 Reply Last reply
    0
    • C Chiman1

      I am migrating a C++ application to .NET. In C++ ,I have an template class which uses some ATL library classes for eg. template< class T > class MyBaseClass: public IConnectionPointContainerImpl, public IPersistStorageImpl, public IPersistPropertyBagImpl, { } Now i need to use this call in my new C# .net project where my new .NET call will get derive from above MyBaseClass . Please let me know how can i archive this ??? Is this possible to use a template class which uses some ATL libraray classes directly in C# ? `chiman

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      There is no straightforward method to use template classes in other CLI languages like C#. This is because of the difference in how C++ templates and CLI generics works. C++ templates will be instantiated at compile time and CLI generics will be on runtime. C++/CLI also supports managed templates, but unfortunately it is not portable to other CLI languages. If you want to use inheritance, you may need to create managed classes that calls the template class for each known type. Like MyBaseClassInt which use MyBaseClass<int> etc. This will not be the correct method as you will have class explosion when you have several types to be used. Prefer composition over inheritance. In such case, this can be easily solved by providing a factory class which checks the type and instantiates respective template instance. Any new types can be easily added to the factory method without recompiling the whole application. We are taking the capability of C++/CLI to mix generic and template type parameters. To see how this can be done, consider the following code:

      // Your existing class - Keep it intact

      template< class T >
      class MyBaseClass:
      public IConnectionPointContainerImpl,
      public IPersistStorageImpl,
      public IPersistPropertyBagImpl,

      {
      public:
      void DoSomething();
      };

      // Define a managed generic interface that all CLI languages can use
      // This interface should have all the methods that other languages need access
      // Note that this uses generics which is supported by CLI

      generic<typename T>
      public interface class IManagedBase
      {
      public:
      void DoSomething();
      };

      // This is implementation for the above interface
      // This uses managed template class and work with mixing generic type and template type

      template<typename T>
      ref class MyBaseClassWrapper : IManagedBase<T>
      {
      public:
      virtual void DoSomething()
      {
      // Using native class
      MyBaseClass<T> native;
      native.DoSomething();
      }
      };

      // Provide a simple builder class which takes care about instantiations and returns interface
      // This will be used by other CLI languages

      public ref class NativeObjectBuilder
      {
      public:
      // Add all the types you know here
      generic<typename T> virtual IManagedBase<T> GetBaseObject()
      {
      // typeid gives the compile time type
      if(T::typeid == int::typeid)
      return (IManagedBase<T>^)

      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