Deriving a class from string

P

Paulo da Silva

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.
 
J

Johannes Schaub (litb)

Paulo said:
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.
 
P

Paulo da Silva

Em 05-12-2010 14:35, Daniel T. escreveu:
Sam said:
Paulo said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top