class wrappers

This is a discussion on class wrappers within the C++ Programming forums, part of the General Programming Boards category; Hello, I am quite new to C++, would someone explain / provide link about class wrappers: what are they, when ...

  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,796
    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,796
    >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, 08: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
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21