Thread: class wrappers

  1. #1
    cristian_negresco
    Guest

    class wrappers

    Hello,

    I am quite new to C++, would someone explain / provide link about class wrappers: what are they, when are they used, when not?
    Thank you.

    Cristian

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A wrapper class extends the functionality of another class, or provides an interface that is easier to use. A good example is if you have a class that prints text. It takes strings as input and writes them to a chosen output:
    Code:
    class PrintText
    {
    public:
      void print ( string text, fstream file )
      {
        // Print text to file
      }
    };
    Now say we want to translate the text to german and then print it. We could add the functionality to the PrintText class, but a more elegant solution would be to wrap a translation class around it:
    Code:
    class PrintText
    {
    public:
      void print ( string text, fstream file )
      {
        // Print text to file
      }
    };
    
    class GermanPrintText
    {
      PrintText pt;
      string    trans_buffer;
    public:
      GermanPrintText ( string text )
      {
        trans_buffer = text;
      }
      void translate( fstream file )
      {
        // Translate text
        pt.print ( trans_buffer, file );
      }
    };
    That's the basic idea.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    Why use a wrapper as opposed to a derived class?

    class GermanPrintText : public PrintText

    fletch
    "Logic is the art of going wrong with confidence."
    Morris Kline

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why use a wrapper as opposed to a derived class?
    Style and elegance. I personally would rather write a wrapper than extend the inheritance tree. In C++ I can't think of any reason to use one over the other, so it's a question of personal preference.

    -Prelude
    My best code is written with the delete key.

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    What if the original class didn't have virtual methods (including a virtual destructor)? Then inheritance is a nono, derived classes can't be destroyed safely with a pointer to a base class.

    What if you want to change a classes interface, say, implement a stack with a vector? Inheritance preserves the old interface, but random access doesn't make sense with a stack.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM