Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Deriving a class from string

Reply
Thread Tools

Deriving a class from string

 
 
Paulo da Silva
Guest
Posts: n/a
 
      12-05-2010
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.
 
Reply With Quote
 
 
 
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      12-05-2010
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.
 
Reply With Quote
 
 
 
 
Paulo da Silva
Guest
Posts: n/a
 
      12-05-2010
Em 05-12-2010 14:35, Daniel T. escreveu:
> Sam <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
deriving a class from a base class markww C++ 1 08-22-2006 11:55 AM
Problem deriving from WebControl Class Gary Rynearson ASP .Net 0 11-18-2005 03:34 PM
Deriving abstract class from non-abstract class Matthias Kaeppler Java 1 05-22-2005 01:28 PM
Problem in deriving custome class from XmlNode Mahesh Devjibhai Dhola ASP .Net 0 10-15-2004 07:38 AM
Deriving a regular class from a template? Steven T. Hatton C++ 28 04-28-2004 10:22 AM



Advertisments