"inheriting" constructors

Discussion in 'C++' started by Christof Warlich, Jul 18, 2007.

  1. Hi,

    I'd like to define a class that should behave as much as posible
    like std::string, but that has some small additional property:

    class ExtendedString: public std::string {
    public:
    void someExtendedFunctionality(void) {}
    };

    As new classes do not inherit the constructors from its base, is
    it true that I would have to define counterparts for all std::string
    constructors that I may want to use to construct my derived class?

    class ExtendedString: public std::string {
    public:
    void someExtendedFunctionality(void) {}
    ExtendedString(const char *data): std::string(data) {}
    ExtendedString(const std::string &data): std::string(data) {}
    ExtendedString(void): std::string() {}
    // ......
    };

    Or is there some smarter way to tell the compiler that the constructors
    of std::string may be used right away, saving all the typing effort?

    Thanks for any tips,

    Christof
    Christof Warlich, Jul 18, 2007
    #1
    1. Advertising

  2. Christof Warlich

    Kai-Uwe Bux Guest

    Christof Warlich wrote:

    > Hi,
    >
    > I'd like to define a class that should behave as much as posible
    > like std::string, but that has some small additional property:
    >
    > class ExtendedString: public std::string {
    > public:
    > void someExtendedFunctionality(void) {}
    > };
    >
    > As new classes do not inherit the constructors from its base, is
    > it true that I would have to define counterparts for all std::string
    > constructors that I may want to use to construct my derived class?
    >
    > class ExtendedString: public std::string {
    > public:
    > void someExtendedFunctionality(void) {}
    > ExtendedString(const char *data): std::string(data) {}
    > ExtendedString(const std::string &data): std::string(data) {}
    > ExtendedString(void): std::string() {}
    > // ......
    > };
    >
    > Or is there some smarter way to tell the compiler that the constructors
    > of std::string may be used right away, saving all the typing effort?


    a) You can use templated constructors:

    ExtendedString() : std::string() {}

    template < typename A >
    ExtendedString( A a ) : std::string( a ) {}

    With std::string, I think you need to go up to 4 arguments.


    b) It appears that your ExtendedString has no additional data members and
    that the functionality provided by

    void someExtendedFunctionality(void) {}

    could as well be provided by a free-standing function. If so, a
    free-standing functions is probably more appropriate. Maybe, you can even
    come up with a generic implementation working on character-ranges. In that
    case, you would have a more flexible addition to your library with less
    code.


    c) Many people frown upon public derivation from container classes. In my
    opinion, it's not a deadly sin; but you should be aware of the possible
    pitfalls:

    1) Keep in mind that the destructor of std::string is non-virtual.

    2) Note that functions like

    std::string some_func( std::string const & str );

    will happily accept ExtendedString objects as arguments, but they
    will return std::string output. There are situations, where this
    can get tricky.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jul 18, 2007
    #2
    1. Advertising

  3. Kai-Uwe Bux schrieb:

    > a) You can use templated constructors:
    >
    > ExtendedString() : std::string() {}
    >
    > template < typename A >
    > ExtendedString( A a ) : std::string( a ) {}
    >
    > With std::string, I think you need to go up to 4 arguments.


    Thanks, that was the type of idea I was looking for. Although
    it's a pity that it still needs one definition for each number
    of parameters that shall be supported ...

    > c) Many people frown upon public derivation from container classes. In my
    > opinion, it's not a deadly sin; but you should be aware of the possible
    > pitfalls:
    >
    > 1) Keep in mind that the destructor of std::string is non-virtual.
    >
    > 2) Note that functions like
    >
    > std::string some_func( std::string const & str );
    >
    > will happily accept ExtendedString objects as arguments, but they
    > will return std::string output. There are situations, where this
    > can get tricky.


    But even more thanks for pointing out the pitfalls!
    Christof Warlich, Jul 18, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,250
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    574
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    583
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    383
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    456
Loading...

Share This Page