Deriving a class from string

Discussion in 'C++' started by Paulo da Silva, Dec 5, 2010.

  1. Hi.

    I need a class with several string methods. I don't want to use the
    string class itself because in future I may want to redefine it to use
    other ways to implement the same (needed) methods but with a different
    behaviour.

    I tried

    class myClass: public string
    { public:
    using string::string;
    }

    This seems not to be recognized by g++.

    What is the minimum code to have all string methods available in
    myClass? Do I have to redefine all constructors I need to use?

    Thanks for any help.
     
    Paulo da Silva, Dec 5, 2010
    #1
    1. Advertising

  2. Paulo da Silva wrote:

    > Hi.
    >
    > I need a class with several string methods. I don't want to use the
    > string class itself because in future I may want to redefine it to use
    > other ways to implement the same (needed) methods but with a different
    > behaviour.
    >
    > I tried
    >
    > class myClass: public string
    > {public:
    > using string::string;
    > }
    >
    > This seems not to be recognized by g++.
    >
    > What is the minimum code to have all string methods available in
    > myClass? Do I have to redefine all constructors I need to use?
    >


    This will work in C++0x (as currently specified by the draft) with the exact
    syntax you use there.
     
    Johannes Schaub (litb), Dec 5, 2010
    #2
    1. Advertising

  3. Em 05-12-2010 14:35, Daniel T. escreveu:
    > Sam <> wrote:
    >> Paulo da Silva writes:
    >>
    >>> I need a class with several string methods. I don't want to use the
    >>> string class itself because in future I may want to redefine it to
    >>> use other ways to implement the same (needed) methods but with a
    >>> different behaviour.

    >
    > [snip]
    >
    >>> What is the minimum code to have all string methods available in
    >>> myClass?

    >>
    >> class myClass : public std::string {
    >>
    >> };
    >>
    >> Feel free to use myClass::substr(), myClass::begin(), myClass::end(), etc…
    >> as expected.

    >
    > ...


    > I suggest you use private inheritance instead of public. Implement the
    > constructors you need and export the minimum number of string functions
    > you need with using declarations.
    >

    Thanks.
    That was, let's say, the classic way. I just wanted, if possible,
    something simpler since I need almost the same behaviour of a complex
    class except for very few methods.
     
    Paulo da Silva, Dec 5, 2010
    #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. Mahesh Devjibhai Dhola

    Problem in deriving custome class from XmlNode

    Mahesh Devjibhai Dhola, Oct 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    517
    Mahesh Devjibhai Dhola
    Oct 15, 2004
  2. Gary Rynearson

    Problem deriving from WebControl Class

    Gary Rynearson, Nov 18, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    432
    Gary Rynearson
    Nov 18, 2005
  3. Matthias Kaeppler
    Replies:
    1
    Views:
    470
    R.F. Pels
    May 22, 2005
  4. Steven T. Hatton

    Deriving a regular class from a template?

    Steven T. Hatton, Apr 23, 2004, in forum: C++
    Replies:
    28
    Views:
    2,224
    Richard Herring
    Apr 28, 2004
  5. markww
    Replies:
    1
    Views:
    303
    Ivan Vecerina
    Aug 22, 2006
Loading...

Share This Page