473,413 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Subclassing string class

Ray

Hi all,

I am thinking of subclassing the standard string class so I can do
something like:

mystring str;
....
str.toLower ();

A quick search on this newsgroup has found messages by others
suggesting that subclassing a standard class is a bad idea, especially
for beginners...

Can someone suggest what is the recommended way to add functionality
to (say) the string class? Actually, I would like to do other things
to strings, and toLower is just one of them. Is it better or is the
only way to just perform a function call:

toLower (string s)

[Of course, there is a C function with the same name, so something
similar to it.]

Another way is to create a class with a private member variable "data"
of type string I should add that I'm somewhat motivated by efficiency
as these tasks with strings will be performed very often.

Which of these two is recommended? Or is there a third that I have
not thought of?

Thank you!

Ray

Jul 15 '08 #1
5 2493
Ray wrote:
I am thinking of subclassing the standard string class so I can do
something like:

mystring str;
...
str.toLower ();

A quick search on this newsgroup has found messages by others
suggesting that subclassing a standard class is a bad idea, especially
for beginners...
I actually don't even like the term "subclassing"...
Can someone suggest what is the recommended way to add functionality
to (say) the string class? Actually, I would like to do other things
to strings, and toLower is just one of them. Is it better or is the
only way to just perform a function call:

toLower (string s)
string toLower(string const& s);
>
[Of course, there is a C function with the same name, so something
similar to it.]
There is no C function with the same name. It's 'tolower' (no capital
letter).
Another way is to create a class with a private member variable "data"
of type string I should add that I'm somewhat motivated by efficiency
as these tasks with strings will be performed very often.

Which of these two is recommended? Or is there a third that I have
not thought of?
Stand-alone functions are better than members of the class deriving from
the standard class. At least with those you could expect application of
conversions (standard and user-defined).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 15 '08 #2
Alf P. Steinbach wrote:
* Victor Bazarov:
>>
Stand-alone functions are better than members of the class deriving
from the standard class. At least with those you could expect
application of conversions (standard and user-defined).

Huh?
If you have a function that can uppercase a string, it's better if you
make it a non-member because a class that defines a conversion to a
string, cannot be used with a member.

Consider

class M {};
void foo(M&);
class L { public: operator M&(); };

versus

class M { public: foo(); };
class L { public: operator M&(); };

With the former you can have

L l;
foo(l);

but with the latter you can't do

L l;
l.foo();
>
Consider

struct M {};
void foo( M& );

versus

struct M { void foo(); };

With the former you can write

M o;
foo( o );

With the latter you can write

M().foo();

If foo is an operator like << this can be of practical significance.
Huh?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 15 '08 #3
Ray wrote:
Can someone suggest what is the recommended way to add functionality
to (say) the string class? Actually, I would like to do other things
to strings, and toLower is just one of them.
Why not use the String Algorithms library in Boost?
http://www.boost.org/doc/libs/1_35_0...ring_algo.html

It may already offer most of the functionality you need, including case
conversion algorithms.

std::string source = "FooBar";
boost::algorithm::to_lower(source);
std::cout << source; //prints "foobar"

Note how the library does *not* derive from std::string but uses
non-member functions that take std::string parameters.
--
Christian Hackl
Jul 15 '08 #4
Ray

Hi Victor/Alf,

Thank you both for your replies. Actually, I had gotten a little
further than what I posted and stumbled on exactly what you two are
now discussing...

I derived a class mystring (sorry, I won't say "subclass" :-) ) from
string and was trying to do:

mystring foo
....
foo.length ();

I didn't think about casting...but I thought somehow that should have
worked. Obviously, not... Thanks for the two suggestions and the
pros/cons of each!

Ray

On Jul 16, 6:25 am, "Alf P. Steinbach" <al...@start.nowrote:
* Victor Bazarov:
Alf P. Steinbach wrote:
* Victor Bazarov:
class M {};
void foo(M&);
class L { public: operator M&(); };
versus
class M { public: foo(); };
class L { public: operator M&(); };
With the former you can have
L l;
foo(l);
but with the latter you can't do
L l;
l.foo();

Reference operators are bad 'uns generally, but for latter you could just
static_cast<M&>(l).foo(), and even static_cast<M&>(L()).foo().

But I think the above misses the point, by introducing that conversion to reference.
Jul 16 '08 #5
Ray

Hi Christian,

Thanks for this -- I didn't know that the Boost library had string
algorithms. I should have thought that something like "tolower" would
be there. I think I'll take this option; I'm obviously too new to
continue going down the current path. :-) Thank you!

Ray

On Jul 16, 7:13 am, Christian Hackl <ha...@sbox.tugraz.atwrote:
Ray wrote:
Can someone suggest what is the recommended way to add functionality
to (say) the string class? Actually, I would like to do other things
to strings, and toLower is just one of them.

Why not use the String Algorithms library in Boost?http://www.boost.org/doc/libs/1_35_0...ring_algo.html

It may already offer most of the functionality you need, including case
conversion algorithms.

std::string source = "FooBar";
boost::algorithm::to_lower(source);
std::cout << source; //prints "foobar"

Note how the library does *not* derive from std::string but uses
non-member functions that take std::string parameters.

--
Christian Hackl
Jul 16 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fuzzyman | last post by:
I recently went through a bit of a headache trying to subclass string.... This is because the string is immutable and uses the mysterious __new__ method rather than __init__ to 'create' a string....
2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
11
by: Brent | last post by:
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data
7
by: Guinness Mann | last post by:
I have a class that I use throughout my application, and I store collections of my class in an ArrayList. To avoid some really ugly casting I'd like to subclass ArrayList to get a typed version. ...
2
by: Laura Martignas | last post by:
Hi everybody I'm trying to subclass Winamp's main window in order to develop a software which will interact with Winamp. For testing, I'm trying to intercept the closing of this window via...
0
by: Dijkstra | last post by:
Hi! I am writing a program in which I need to keep track of how many objects of a given class exists at a time. The task is to do it using templates. The objects to be counted are of the class Note:...
16
by: manatlan | last post by:
I've got an instance of a class, ex : b=gtk.Button() I'd like to add methods and attributes to my instance "b". I know it's possible by hacking "b" with setattr() methods. But i'd like to do...
3
by: Larry Bates | last post by:
I'm trying to learn about subclassing new style classes and the first project I went to do needs to subclass zipfile to add some methods. Why does this: import zipfile class...
13
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to figure out how to create subclasses with properties specific to the subclass and so far it isn't going well. Right now I have a class with an enum representing the type. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.