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. C / C++ / MFC
  4. Exporting template class

Exporting template class

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
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.
  • N Offline
    N Offline
    Nilesh K
    wrote on last edited by
    #1

    Hi, I'm unable to export a template class from an MFC based dll ! any ideas of how I can do it ? Ciao - Nilesh

    C P J 3 Replies Last reply
    0
    • N Nilesh K

      Hi, I'm unable to export a template class from an MFC based dll ! any ideas of how I can do it ? Ciao - Nilesh

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Template classes are generated by their template arguments, they are in fact a little class factory. So I reckon you'll have trouble. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
      C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
      Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002

      1 Reply Last reply
      0
      • N Nilesh K

        Hi, I'm unable to export a template class from an MFC based dll ! any ideas of how I can do it ? Ciao - Nilesh

        P Offline
        P Offline
        Paul M Watt
        wrote on last edited by
        #3

        You can export a template class as long as you force the compiler to create an instance of the class that you are trying to export. That is because a template is a compile time feature. You can force the compiler to generate the code for a particular type of object by simply declaring a global variable of that particular type. But for each type you declare the size of your DLL will grow. There is no way to export a template from a DLL, an retain the power that a template provides. Would it simply be possible for you to distribute the header file with the template class in it instead?


        Build a man a fire, and he will be warm for a day
        Light a man on fire, and he will be warm for the rest of his life!

        1 Reply Last reply
        0
        • N Nilesh K

          Hi, I'm unable to export a template class from an MFC based dll ! any ideas of how I can do it ? Ciao - Nilesh

          J Offline
          J Offline
          Jeff J
          wrote on last edited by
          #4

          I once did what you are trying to do (for export to a non-C++ project), but as Paul mentioned, the benefits of templates are lost. One way is to create a non-templatised wrapper class, and wrap every single method so that it calls the template version. Make sure you inline everything to avoid extra call overhead. If you forget to wrap any methods, the compiler will not generate them at all. Of course, you will have to declare one specific type to be used with the exported class, hence the class will no longer be generic. You can create separate wrapper classes for different types, though your DLL will get rather large. Cheers

          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