Re: [Dev-C++] Super charging the string class
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Graham D. <g.d...@nt...> - 2003-07-04 21:13:17
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title></title>
</head>
<body>
<br>
<br>
David McKen wrote:<br>
<blockquote type="cite"
cite="mid...@we...">
<title>Message</title>
<meta http-equiv="Content-Type" content="text/html; ">
<meta content="MSHTML 6.00.2800.1170" name="GENERATOR">
<style></style>
<div><font face="Arial" size="2">I see. So sub-classing
basic_string<char> would be the reccomended way to do this then?</font></div>
</blockquote>
No, no, no. basic_string is <span style="font-style: italic;">not</span>
designed for inheritance. It has no virtual functions and it has no
virtual destructor. Public inheritance from such a class is a major
maintenance nightmare waiting to happen. If you wish to extend the
functionality of basic_string, layer it - put in a class member of type
basic_string. You will have to re-specify all the functions from
basic_string that you want to use by stating them in your new class's
public interface and then passing them on to the basic_string member;
but even that amount of work is trivial compared to the potential
headaches that come from publically inheriting from a class that isn't
designed for it. And it's not just me saying it, Scott Meyers in <span
style="font-style: italic;">Effective C++</span> devotes an entire
item to warning of the dangers, Herb Sutter has written a number of
articles detailing why you shouldn't do; I think I saw something from
Stroustrup about it as well. All the experts agree: don't do it!<br>
<blockquote type="cite"
cite="mid...@we...">
<div> </div>
<div><font face="Arial" size="2">With respect to the over-ridden
methods I am wondering whether you have some example code that I can
see. I know how to call base class methods from a derived class but
what I need is a way to reverse the default method called (usually the
derived method is called, I want the base to be called, if one exist)</font></div>
<blockquote dir="ltr"
style="border-left: 2px solid rgb(0, 0, 0); padding-right: 0px; padding-left: 5px; margin-left: 5px; margin-right: 0px;">
<div
style="font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">-----
Original Message ----- </div>
<div
style="background: rgb(228, 228, 228) none repeat scroll 0%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><b>From:</b>
<a title="iv...@em..." href="mailto:iv...@em...">Ioannis Vranos</a>
</div>
<div
style="font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><b>To:</b>
<a title="dev...@li..."
href="mailto:dev...@li...">'Dev-Cpp Mailing
List'</a> </div>
<div
style="font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><b>Sent:</b>
Friday, July 04, 2003 1:19 PM</div>
<div
style="font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><b>Subject:</b>
RE: [Dev-C++] Super charging the string class</div>
<div><br>
</div>
<div><strong></strong> </div>
<blockquote
style="border-left: 2px solid rgb(0, 0, 128); padding-left: 5px; margin-left: 5px; margin-right: 0px;">
<div class="OutlookMessageHeader" lang="en-us" dir="ltr"
align="left"><font face="Tahoma" size="2">-----Original Message-----<br>
<b>From:</b> <a
href="mailto:dev...@li...">dev...@li...</a>
[<a class="moz-txt-link-freetext" href="mailto:dev...@li...">mailto:dev...@li...</a>] <b>On Behalf Of </b>David
McKen<br>
<b>Sent:</b> Friday, July 04, 2003 7:51 PM<br>
<b>To:</b> Dev-Cpp Mailing List<br>
<b>Subject:</b> [Dev-C++] Super charging the string class<br>
<br>
</font></div>
<div><font face="Arial" size="2">I am currently writing a heavy
text-processing application.</font></div>
<div> </div>
<div><font face="Arial" size="2">As far as I can see the string
class is not implemented uniformly. I wish to fill in the gaps as well
as add a few extras that are used on a regular basis in the program
(not the least of which are overloading the =, << and +=
operators).</font></div>
<div> </div>
<div><font face="Arial" size="2">What would be the best way to do
this?</font></div>
<div> </div>
<div><font face="Arial"><font size="2">I was thinking create a
class that inhereted from the string class and make my changes to that
class. The only thing I am a bit curious about with this method is what
happens if I override a function that exist on one of the platforms.
How can I check for it's existence at compile time (Odds are the
compiler writer's implementation is faster or more stable than mine).<span
class="706580917-04072003"><strong><font color="#000080"> </font></strong></span></font></font></div>
</blockquote>
<div><strong></strong> </div>
<div><strong><font face="Arial"><font size="2"><font color="#000080"><font
face="Arial"><font size="2"><span class="706580917-04072003">std::string
is basically a typedef of basic_string<char>. That is , it is:</span></font></font></font></font></font></strong></div>
<div><strong></strong> </div>
<div><strong><font face="Arial"><font size="2"><font color="#000080"><font
face="Arial"><font size="2"><span class="706580917-04072003">typedef
basic_string<char> string;</span></font></font></font></font></font></strong></div>
<div><strong></strong> </div>
<div><strong></strong> </div>
<div><strong><font face="Arial"><font size="2"><font color="#000080"><font
face="Arial"><font size="2"><span class="706580917-04072003">You can
inherit from both, but i assume you intent to inherit
basic_string<char>.</span></font></font></font></font></font></strong></div>
<div><strong></strong> </div>
<div><strong></strong> </div>
<div><strong><font face="Arial"><font size="2"><font color="#000080"><font
face="Arial"><font size="2"><span class="706580917-04072003">If you
override an already existing function it will happen as always. Your
inherited class member function will be executed for your inherited
class instances (objects). As always however if you use a base pointer
(pointer to std::string) the string member function will be executed.</span></font></font></font></font></font></strong></div>
<div><strong></strong> </div>
<div><strong></strong> </div>
<div><strong><font face="Arial"><font size="2"><font color="#000080"><font
face="Arial"><font size="2"><span class="706580917-04072003">Another
way is to have an std::string object as a data member in your new type
but in this case you would have to provide the whole string interface.</span></font></font></font></font></font></strong></div>
<div><strong></strong> </div>
<div><strong></strong> </div>
<div><strong><font face="Arial"><font size="2"><font color="#000080"><font
face="Arial"><font size="2"><span class="706580917-04072003">To
see the basic_string members you can check the stanndard, TC++PL 3, or
the web.</span></font></font></font></font></font></strong></div>
<div><strong></strong> </div>
<div><strong></strong> </div>
<div><strong></strong> </div>
<blockquote
style="border-left: 2px solid rgb(0, 0, 128); padding-left: 5px; margin-left: 5px; margin-right: 0px;">
<div><font face="Arial"><font size="2">Another thing I saw and
would like an opinion on is making all my overloads global. (i.e.
instead of being part of the class they are normal functions thet
implement the overloads). I see this getting a bit unweildly though.<span
class="706580917-04072003"><strong><font color="#000080"> </font></strong></span></font></font></div>
</blockquote>
<div><font size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><strong></strong></span></font></font> </div>
<div> </div>
<div><font size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"> <strong>In any case you should place your
new string type inside a namespace. In this way you can even use the
name string for your class!</strong></span></font></font></div>
<div><font size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><strong></strong></span></font></font> </div>
<div><font size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><strong>As a general rule of style,
definitions of operators having to do with objects of the same class
are placed inside the class, and operators having to do with objects of
other types are placed as "global" (in the namespace).</strong></span></font></font></div>
<div><font size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><strong></strong></span></font></font> </div>
<div><font size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><strong>TC++PL tells about this somewhere
(as it does for comment style using // for comments and /* */ only for
debugging).</strong></span></font></font></div>
<div><font size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><strong></strong></span></font></font> </div>
<div><font size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><strong>I summary the master in TC++PL
talks about everything! :-)</strong></span></font></font></div>
<div><font size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><strong></strong></span></font></font> </div>
<div><strong><font face="Arial"><font size="2"><font color="#000080"><font
size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><strong></strong></span></font></font></font></font></font></strong> </div>
<div><strong><font face="Arial"><font size="2"><font color="#000080"><font
size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><strong></strong></span></font></font></font></font></font></strong> </div>
<div><strong><font face="Arial"><font size="2"><font color="#000080"><font
size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><strong></strong></span></font></font></font></font></font></strong> </div>
<div><strong><font face="Arial"><font size="2"><font color="#000080"><font
size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><strong></strong></span></font></font></font></font></font></strong> </div>
<div><strong><font face="Arial"><font size="2"><font color="#000080"><font
size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><strong></strong></span></font></font></font></font></font></strong> </div>
<div><font size="+0"><font face="Arial" color="#000080" size="2"><span
class="706580917-04072003"><font size="2"><strong>Ioannis Vranos<br>
<br>
* Programming pages: </strong><a href="http://www.noicys.freeurl.com/"><strong>http://www.noicys.freeurlcom</strong></a><br>
<strong>* Alternative URL 1: </strong><a
href="http://run.to/noicys"><strong>http://run.to/noicys</strong></a><br>
<strong>* Alternative URL 2: </strong><a
href="http://www.noicys.cjb.net/"><strong>http://www.noicys.cjb.net</strong></a></font>
</span></font></font></div>
<font size="+0"><font face="Arial" color="#000080" size="2"> </font></font>
<div> </div>
</blockquote>
<hr size="1">Do you Yahoo!?<br>
<a
href="http://us.rd.yahoo.com/search/mailsig/*http://search.yahoo.com">The
New Yahoo! Search</a> - Faster. Easier. Bingo.
</blockquote>
</body>
</html>
|