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. C++ Template question

C++ Template question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++json
9 Posts 3 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.
  • R Offline
    R Offline
    rrrado
    wrote on last edited by
    #1

    I need to crate template class which will be generic parent of more classes containing data. Each child class defines something like item ID (different name in each class, but always int) and I need to let parent class know it's name to be able to make some operations on that. This is pseudocode:

    template <class T, ???XXX > class DRecord
    {
    void DoReset() { ((T*)this) -> XXX = 0; } // accessing the ID
    }

    class A : public DRecord<A, ???A_XXX >
    {
    int A_XXX;
    }

    XXX is template param I need to define and use some way. I've tried to use member pointers but it didn't compile. Is it possible to define member of child class as parameter into parent class? In moment of parsing the template the member variable is not declared yet, so I'm not sure whether is it possible. Thank you.

    C S 2 Replies Last reply
    0
    • R rrrado

      I need to crate template class which will be generic parent of more classes containing data. Each child class defines something like item ID (different name in each class, but always int) and I need to let parent class know it's name to be able to make some operations on that. This is pseudocode:

      template <class T, ???XXX > class DRecord
      {
      void DoReset() { ((T*)this) -> XXX = 0; } // accessing the ID
      }

      class A : public DRecord<A, ???A_XXX >
      {
      int A_XXX;
      }

      XXX is template param I need to define and use some way. I've tried to use member pointers but it didn't compile. Is it possible to define member of child class as parameter into parent class? In moment of parsing the template the member variable is not declared yet, so I'm not sure whether is it possible. Thank you.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Why do you want to do that? Could just use polimorphism, couldn't you? :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      R 1 Reply Last reply
      0
      • C CPallini

        Why do you want to do that? Could just use polimorphism, couldn't you? :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        R Offline
        R Offline
        rrrado
        wrote on last edited by
        #3

        I'm creating some database wrapper, it maps tables to classes (or structures). There are some common operations I need to do on primary keys, but each table is using different PK names. I want to code common operations only once (using templates, not preprocessor macros unless necessary) and don't want to use virtual functions (it should be theoretically possible, but I don't know whether C++ templates are powerful enough for this). It is also interesting problem for me since I'm not template expert and I'd like to learn :)

        C 1 Reply Last reply
        0
        • R rrrado

          I'm creating some database wrapper, it maps tables to classes (or structures). There are some common operations I need to do on primary keys, but each table is using different PK names. I want to code common operations only once (using templates, not preprocessor macros unless necessary) and don't want to use virtual functions (it should be theoretically possible, but I don't know whether C++ templates are powerful enough for this). It is also interesting problem for me since I'm not template expert and I'd like to learn :)

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          rrrado wrote:

          and don't want to use virtual functions

          Why don't you (you know base classes practically imply this... :rolleyes: )? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          R 1 Reply Last reply
          0
          • C CPallini

            rrrado wrote:

            and don't want to use virtual functions

            Why don't you (you know base classes practically imply this... :rolleyes: )? :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            R Offline
            R Offline
            rrrado
            wrote on last edited by
            #5

            Because it is not necessary to use virtual tables just to make simple job :) Just for one function <pre>virtual int& GetID();</pre> There are more ways how to do this but I'd like to use clean solution. I have solution with templates for this but it is using extra class and multiple inheritance so I don't like it. This could be useful for me also in future for different purposes so I'd like to know :)

            C 1 Reply Last reply
            0
            • R rrrado

              Because it is not necessary to use virtual tables just to make simple job :) Just for one function <pre>virtual int& GetID();</pre> There are more ways how to do this but I'd like to use clean solution. I have solution with templates for this but it is using extra class and multiple inheritance so I don't like it. This could be useful for me also in future for different purposes so I'd like to know :)

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              rrrado wrote:

              Because it is not necessary to use virtual tables just to make simple job Smile Just for one function

              virtual int& GetID();

              Since this is your point of view, why do you ask for a 'clean solution'? :rolleyes: :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              1 Reply Last reply
              0
              • R rrrado

                I need to crate template class which will be generic parent of more classes containing data. Each child class defines something like item ID (different name in each class, but always int) and I need to let parent class know it's name to be able to make some operations on that. This is pseudocode:

                template <class T, ???XXX > class DRecord
                {
                void DoReset() { ((T*)this) -> XXX = 0; } // accessing the ID
                }

                class A : public DRecord<A, ???A_XXX >
                {
                int A_XXX;
                }

                XXX is template param I need to define and use some way. I've tried to use member pointers but it didn't compile. Is it possible to define member of child class as parameter into parent class? In moment of parsing the template the member variable is not declared yet, so I'm not sure whether is it possible. Thank you.

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                How about just presuming the existence of accessor functions in the parent - I've defined a macro to help reduce the amount of typing:

                template <class T> class DRecord
                {
                void DoReset() { static_cast<T*>(this)->SetID(0); } // accessing the ID
                };

                #define DEFINE_ID_FIELD(ID_NAME) \
                int NAME;
                void SetID(int value) { NAME = value; }
                int GetID(int value) const { return NAME; }

                class A : public DRecord<A>
                {
                DEFINE_ID_FIELD(A_XXX)
                };

                As an aside - are you aware of the OLE DB consumer templates[^] - they're templated database wrappers...

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                R 1 Reply Last reply
                0
                • S Stuart Dootson

                  How about just presuming the existence of accessor functions in the parent - I've defined a macro to help reduce the amount of typing:

                  template <class T> class DRecord
                  {
                  void DoReset() { static_cast<T*>(this)->SetID(0); } // accessing the ID
                  };

                  #define DEFINE_ID_FIELD(ID_NAME) \
                  int NAME;
                  void SetID(int value) { NAME = value; }
                  int GetID(int value) const { return NAME; }

                  class A : public DRecord<A>
                  {
                  DEFINE_ID_FIELD(A_XXX)
                  };

                  As an aside - are you aware of the OLE DB consumer templates[^] - they're templated database wrappers...

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  R Offline
                  R Offline
                  rrrado
                  wrote on last edited by
                  #8

                  Thank you for idea, I was thinking about it and haven't realized that I don't need virtual accessors for this unless I saw your code. I don't know OLE DB templates, thank you for tip. I'm using sqlite so I guess I cannot use them but maybe there will be some good point in their architecture.

                  S 1 Reply Last reply
                  0
                  • R rrrado

                    Thank you for idea, I was thinking about it and haven't realized that I don't need virtual accessors for this unless I saw your code. I don't know OLE DB templates, thank you for tip. I'm using sqlite so I guess I cannot use them but maybe there will be some good point in their architecture.

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #9

                    rrrado wrote:

                    I don't know OLE DB templates, thank you for tip. I'm using sqlite so I guess I cannot use them...

                    I wouldn't bet on it - OLE DB Provider for SQLite[^]

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    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