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. CPP question

CPP question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
10 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.
  • A Offline
    A Offline
    alex barylski
    wrote on last edited by
    #1

    template class CBase
    template class CChild : private CBase

    CChild has members which point to a heap and have related Get and Set functions. Is it possible for CBase to call it's child's member functions Get and Set to manipulate the heap...? I've considered passing a pointer to the heap to CBase and just copying the Get/Set functionality into the base class, however this doesn't see to be appropriate and i might as well put everything into one class. Using RTTI is it possible to obtain pointer to CChild from CBase and call it's Set/Get functions on the current heap rather than using my approach...? TIA:) "An expert is someone who has made all the mistakes in thier field" - Niels Bohr

    P J J 3 Replies Last reply
    0
    • A alex barylski

      template class CBase
      template class CChild : private CBase

      CChild has members which point to a heap and have related Get and Set functions. Is it possible for CBase to call it's child's member functions Get and Set to manipulate the heap...? I've considered passing a pointer to the heap to CBase and just copying the Get/Set functionality into the base class, however this doesn't see to be appropriate and i might as well put everything into one class. Using RTTI is it possible to obtain pointer to CChild from CBase and call it's Set/Get functions on the current heap rather than using my approach...? TIA:) "An expert is someone who has made all the mistakes in thier field" - Niels Bohr

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

      HockeyDude wrote: I've considered passing a pointer to the heap to CBase and just copying the Get/Set functionality into the base class, however this doesn't see to be appropriate and i might as well put everything into one class. If CBase needs to operate on that data, why wouldn't you place that data in the CBase class? That is where it seems to belong to me. HockeyDude wrote: Using RTTI is it possible to obtain pointer to CChild from CBase and call it's Set/Get functions on the current heap rather than using my approach...? This can be done, but once again, I believe that the data should probably go into the CBase class itself. Could you give an example of why you want to put this data in the CChild class, and maybe there is a better solution that I could suggest? kilowatt

      A 1 Reply Last reply
      0
      • P Paul M Watt

        HockeyDude wrote: I've considered passing a pointer to the heap to CBase and just copying the Get/Set functionality into the base class, however this doesn't see to be appropriate and i might as well put everything into one class. If CBase needs to operate on that data, why wouldn't you place that data in the CBase class? That is where it seems to belong to me. HockeyDude wrote: Using RTTI is it possible to obtain pointer to CChild from CBase and call it's Set/Get functions on the current heap rather than using my approach...? This can be done, but once again, I believe that the data should probably go into the CBase class itself. Could you give an example of why you want to put this data in the CChild class, and maybe there is a better solution that I could suggest? kilowatt

        A Offline
        A Offline
        alex barylski
        wrote on last edited by
        #3

        CBase is actually CUndoBuffer and CChild is CBuffer CBuffer has Get/Set/Del accessor/mutators friend QSort and is only going to get more complicated as time goes on. CUndoBuffer implements a Push/Pop stack with CanUndo DoRedo DoUndo Clear SetSize etc... It makes easier sense to put em' all into one class, but i think more logical sense to seperate them Cbuffer works with the heap funcitons and is getting pretty complex CundoBuffer works with new/delete and is pretty trivial until realizing DoUndo/DoRedo don't have explicit(?) access to the Child's Redo/Undo I personally would prefer to keep them seperated. "An expert is someone who has made all the mistakes in thier field" - Niels Bohr

        1 Reply Last reply
        0
        • A alex barylski

          template class CBase
          template class CChild : private CBase

          CChild has members which point to a heap and have related Get and Set functions. Is it possible for CBase to call it's child's member functions Get and Set to manipulate the heap...? I've considered passing a pointer to the heap to CBase and just copying the Get/Set functionality into the base class, however this doesn't see to be appropriate and i might as well put everything into one class. Using RTTI is it possible to obtain pointer to CChild from CBase and call it's Set/Get functions on the current heap rather than using my approach...? TIA:) "An expert is someone who has made all the mistakes in thier field" - Niels Bohr

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

          You can adopt the following approach:

          template <class Type, class TChild> class CBase
          {
          ...
          void SomeMethod()
          {
          static_cast<TChild&>(*this).Get(...);
          static_cast<TChild&>(*this).Set(...);
          }
          }:
          template <class Type> class CChild: private CBase<Type, CChild>
          {
          ...
          };

          See the idea? CChild passes its own type to CBase, thus informing the base class about whom child to delegate Get and Set. It's like a compile-time virtual function mechanism. This approach is extensively exploited in ATL. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          A 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            You can adopt the following approach:

            template <class Type, class TChild> class CBase
            {
            ...
            void SomeMethod()
            {
            static_cast<TChild&>(*this).Get(...);
            static_cast<TChild&>(*this).Set(...);
            }
            }:
            template <class Type> class CChild: private CBase<Type, CChild>
            {
            ...
            };

            See the idea? CChild passes its own type to CBase, thus informing the base class about whom child to delegate Get and Set. It's like a compile-time virtual function mechanism. This approach is extensively exploited in ATL. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            A Offline
            A Offline
            alex barylski
            wrote on last edited by
            #5

            Really...this will work..? Sweet...I hope it does, i won't have to recosider my design And it should solve all my problems:-D Really appreciate it!! Thanx again and again "An expert is someone who has made all the mistakes in thier field" - Niels Bohr

            J 1 Reply Last reply
            0
            • A alex barylski

              template class CBase
              template class CChild : private CBase

              CChild has members which point to a heap and have related Get and Set functions. Is it possible for CBase to call it's child's member functions Get and Set to manipulate the heap...? I've considered passing a pointer to the heap to CBase and just copying the Get/Set functionality into the base class, however this doesn't see to be appropriate and i might as well put everything into one class. Using RTTI is it possible to obtain pointer to CChild from CBase and call it's Set/Get functions on the current heap rather than using my approach...? TIA:) "An expert is someone who has made all the mistakes in thier field" - Niels Bohr

              J Offline
              J Offline
              Jens Kreiensiek
              wrote on last edited by
              #6

              hi, what about this approach:

              class CBuffer
              {
              public:
              virtual void Set() = 0;
              virtual void Get() = 0;
              ...
              };

              template<class TBuffer>
              class CUndoBuffer
              {
              public:
              ...
              void SomeFunc()
              {
              m_theBuffer.Set( whatever );
              m_theBuffer.Get( whatever );
              }

              private:
              TBuffer m_theBuffer;
              };

              class CConcreteBuffer : public CBuffer
              {
              public:
              virtual void Set();
              virtual void Get();
              ...
              };

              CUndoBuffer<CConcreteBuffer> AConcreteUndoBuffer;
              etc..

              ..have fun.. jk :cool:

              A 1 Reply Last reply
              0
              • A alex barylski

                Really...this will work..? Sweet...I hope it does, i won't have to recosider my design And it should solve all my problems:-D Really appreciate it!! Thanx again and again "An expert is someone who has made all the mistakes in thier field" - Niels Bohr

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

                Seems like it shouldn't work because of circular references, but the fact is that this little trick compiles fine (and what's more, it does not incur in any run-time penalty, everything is resolved on compilation). To the best of my knowledge, it was the ATL guys the first who used this idiom. Templates are a constant source of enjoyment and productivity. I feel sorry about C# & Java users with its "single hierarchy"/"no hassles"/"type-unsafe containers" languages! Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                A 1 Reply Last reply
                0
                • J Jens Kreiensiek

                  hi, what about this approach:

                  class CBuffer
                  {
                  public:
                  virtual void Set() = 0;
                  virtual void Get() = 0;
                  ...
                  };

                  template<class TBuffer>
                  class CUndoBuffer
                  {
                  public:
                  ...
                  void SomeFunc()
                  {
                  m_theBuffer.Set( whatever );
                  m_theBuffer.Get( whatever );
                  }

                  private:
                  TBuffer m_theBuffer;
                  };

                  class CConcreteBuffer : public CBuffer
                  {
                  public:
                  virtual void Set();
                  virtual void Get();
                  ...
                  };

                  CUndoBuffer<CConcreteBuffer> AConcreteUndoBuffer;
                  etc..

                  ..have fun.. jk :cool:

                  A Offline
                  A Offline
                  alex barylski
                  wrote on last edited by
                  #8

                  Don't pure virtual functions make it a ABC...? CBuffer is not the base class it's the child which contains most of the functionality and would be much easier and I thought OOD friendly if it shared some of it's functionality with it's parent class. Which made me realize I don't understand inheritence, up/downcasting or MI as well as i thought;P The template solution given by Joaquín seems most confusing but has no RT penalties. right now as we speak Joaquín has me convinced, however perhaps you feel another solution is more appropriate. Opinions...? thanx again.:) "An expert is someone who has made all the mistakes in thier field" - Niels Bohr

                  1 Reply Last reply
                  0
                  • J Joaquin M Lopez Munoz

                    Seems like it shouldn't work because of circular references, but the fact is that this little trick compiles fine (and what's more, it does not incur in any run-time penalty, everything is resolved on compilation). To the best of my knowledge, it was the ATL guys the first who used this idiom. Templates are a constant source of enjoyment and productivity. I feel sorry about C# & Java users with its "single hierarchy"/"no hassles"/"type-unsafe containers" languages! Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                    A Offline
                    A Offline
                    alex barylski
                    wrote on last edited by
                    #9

                    Thanx again Joaquín. Not only does it work, but it works great! This totally simplifies the everything in my classes, instead of throwing it all into one big class I've seperated them, which is what was wanted. The 2-3 ideas i originally had required so much more overhead and some redudant code. This is great. Only one question remains. Is this an example of downcasting sort of...? Also, doesn't this break the rules of OOP...? I'm positive this is the best solution for my particular problem, however if used improperly or too often this could cause spaghetti code correct...? Thanx again! "An expert is someone who has made all the mistakes in thier field" - Niels Bohr

                    J 1 Reply Last reply
                    0
                    • A alex barylski

                      Thanx again Joaquín. Not only does it work, but it works great! This totally simplifies the everything in my classes, instead of throwing it all into one big class I've seperated them, which is what was wanted. The 2-3 ideas i originally had required so much more overhead and some redudant code. This is great. Only one question remains. Is this an example of downcasting sort of...? Also, doesn't this break the rules of OOP...? I'm positive this is the best solution for my particular problem, however if used improperly or too often this could cause spaghetti code correct...? Thanx again! "An expert is someone who has made all the mistakes in thier field" - Niels Bohr

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

                      Is this an example of downcasting sort of...? Yes, downcasting is the process of going from a Base * to a Derived *. Also, doesn't this break the rules of OOP...? I don't think so; after all, its semantics strongly mimic that of virtual functions mechanisms (although in compile-time). however if used improperly or too often this could cause spaghetti code correct...? The main defect of this idiom, IMHO, is that it's little known among C++ programmers, which find it hard to understand at first. If it ever goes commonplace in the C++ community, the technique is as sound as many others. Think about this: 20 years ago, this idiom was probably seen as clumsy and tricky:

                      while(*dst++=*src++); /* copy string src to dst */

                      Now it is only one of those gadgets people are accostumed to use in C/C++ code. 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