C++ Proxy Class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kirti222
    New Member
    • Mar 2008
    • 1

    #1

    C++ Proxy Class

    hi what is a Proxy class (in reference to C++)
  • Sreeramu
    New Member
    • Apr 2007
    • 11

    #2
    Can any one give detail about proxy class...

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by kirti222
      hi what is a Proxy class (in reference to C++)
      ... and what did old google have to say?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Sreeramu
        There is northing like proxy class . Proxy would be the name given to the class...can you explain your question in detail...
        Do use google for this. Just google for "proxy C++".

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          A proxy class is a stand-in for another class.

          Let's suppose you have a class that has a method that takes 60 seconds to complete. That means everytime you call that method, your program waits. But let's also assume you rarely call that method. Let's further assume this method is named Load() and the class is MyClass

          [code=cpp]
          class MyClass
          {
          public:
          void Load(); //takes a long time
          void AMethod();
          etc... //the other methods.
          };.
          [/code]

          The proxy class would look like:
          [code=cpp]
          class MyClassProxy
          {
          MyClass* theObject;
          public:
          MyClassProxy(); : theObject(0) {}
          MyClass* operator->();
          MyClass& operator*();
          };

          So when you create a MyClassProxy object, the MyClass* inside is set to zero.

          Now you use MyClassProxy objects instead of MyClass objects.

          If someone needs the MyClass object, they use the operator-> overload of MyClassProxy. This function just returns the MyClass* if the MyClass object exists otherwise is creates it and calls Load().

          [code=cpp]
          MyClass* MyClassProxy::o perator->()
          {
          if (!this->theObject)
          {
          theObject = new MyClass;
          theObject->Load();
          }
          return theObject;
          };
          [/code]

          So not until you use the proxy object with the -> operator do you see the 60 seccond delay.

          [code=cpp]

          MyClassProxy p; //no delay
          p->AMethod(); //Here the MyClass object is created
          //Loaded and the MyClass::AMetho d called.
          [/code]

          Comment

          Working...