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. Template class with unlimited parameters (Advanced)

Template class with unlimited parameters (Advanced)

Scheduled Pinned Locked Moved C / C++ / MFC
c++wpf
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
    conrad Braam
    wrote on last edited by
    #1

    I want to create a template class, with unlimited # parameters, basically I need a special template that is like a collection, but I can access each member directly. I want to create something like this class myclass : public mybaseclass { public: myclass(); DWORD myvalue1; DWORD myvalue2; DWORD myvalue3; }; I need to be able to have a different # of members "myvalue1" etc. each time. the difficult part is that the members must also be used in a function, that is part of the template also myclass::ReadMembers() { myvalue1=1; ... etc } so that I do not have to enter them again, I know macros let U do this kind of thing, but they do not work to well for this application so far. Templates are foweful, but not enough, I even looked at BEGIN_MESSAGE_MAP macro from MFC, because that idea might do it, but it can't as far as I can see.:confused: GRRR.... Maybe I need a 4GL language, not C++. Conrad - conradb@adroit.co.za Always do badly to start off, that way when you get the hang of it suddenly, everyone is surprised.

    J 1 Reply Last reply
    0
    • C conrad Braam

      I want to create a template class, with unlimited # parameters, basically I need a special template that is like a collection, but I can access each member directly. I want to create something like this class myclass : public mybaseclass { public: myclass(); DWORD myvalue1; DWORD myvalue2; DWORD myvalue3; }; I need to be able to have a different # of members "myvalue1" etc. each time. the difficult part is that the members must also be used in a function, that is part of the template also myclass::ReadMembers() { myvalue1=1; ... etc } so that I do not have to enter them again, I know macros let U do this kind of thing, but they do not work to well for this application so far. Templates are foweful, but not enough, I even looked at BEGIN_MESSAGE_MAP macro from MFC, because that idea might do it, but it can't as far as I can see.:confused: GRRR.... Maybe I need a 4GL language, not C++. Conrad - conradb@adroit.co.za Always do badly to start off, that way when you get the hang of it suddenly, everyone is surprised.

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Something like this should work (I don't have a compiler handy, so beware of errors):

      template <int n> class C;
       
      template <> class C<0>
      {
      public:
      void read_members()
      {
      }
      };
       
      template <int n> class C: public C<n-1>
      {
      typedef C<n-1> super;
      public:
      int value;
      void read_members()
      {
      value=1;
      super::read_members();
      }
      };

      Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      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