473,385 Members | 1,593 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,385 software developers and data experts.

C++ Building derived class from sdl string!HELP!

Have to clean up an old class library and have a string class with slightly
different functions. The interface I wrote works quite well except the
operator[]! Actually I only need sdl string operator[] but how can I call
it from the derived class. Someone any idea???

class MyString: public std::string
{
reference operator[](size_type n) {return std::string::operator[](n)}
// doesn't work
char& operator[](size_type n) {return std::string::operator[](n)}
// doesn't work
char& operator[](size_type n) {return *this.operator[](n)}
// doesn't work
}

I'd appreciate any ideas! Need it fast! Thanx for any posting!

DevH
Jul 19 '05 #1
5 2632

"K.S." <sc*****@iue.tuwien.ac.at> wrote in message news:3f**********************@newsreader01.highway .telekom.at...
Have to clean up an old class library and have a string class with slightly
different functions. The interface I wrote works quite well except the
operator[]! Actually I only need sdl string operator[] but how can I call
it from the derived class. Someone any idea???
Deriving from string is a bad idea (and seemingly unnecessary in your
example, you "MyString" class should contain the string and then you
could do
reference operator[](size_type n) { return contained_string[n]); }
class MyString: public std::string
{
reference operator[](size_type n) {return std::string::operator[](n)}
// doesn't work
char& operator[](size_type n) {return std::string::operator[](n)}
// doesn't work
char& operator[](size_type n) {return *this.operator[](n)}
// doesn't work


You forgot the semicolons on all the return statements.

reference operator[](size_type) {
return static_cast<std::string*>(this)->operator[](n); }
Jul 19 '05 #2
Ron Natalie wrote:

"K.S." <sc*****@iue.tuwien.ac.at> wrote in message
news:3f**********************@newsreader01.highway .telekom.at...
Have to clean up an old class library and have a string class with
slightly different functions. The interface I wrote works quite well
except the operator[]! Actually I only need sdl string operator[] but how
can I call it from the derived class. Someone any idea???


Deriving from string is a bad idea (and seemingly unnecessary in your
example, you "MyString" class should contain the string and then you
could do
reference operator[](size_type n) { return contained_string[n]); }

class MyString: public std::string
{
reference operator[](size_type n) {return std::string::operator[](n)}
// doesn't work
char& operator[](size_type n) {return std::string::operator[](n)}
// doesn't work
char& operator[](size_type n) {return *this.operator[](n)}
// doesn't work


You forgot the semicolons on all the return statements.

reference operator[](size_type) {
return static_cast<std::string*>(this)->operator[](n); }


Thanx! This was just the extract that was relevant!! The class is more than
that little bit ;-)! And believe me if you have an old library and some
200.000 lines of code using that old crap its worthwile to write an
interface(even if you have to derive from the sdl String)! ;-)
Second! Yes I forgot the ;! Sorry was typing from memory!

DevH

Jul 19 '05 #3
Ron Natalie wrote:

"K.S." <sc*****@iue.tuwien.ac.at> wrote in message
news:3f**********************@newsreader01.highway .telekom.at...
Have to clean up an old class library and have a string class with
slightly different functions. The interface I wrote works quite well
except the operator[]! Actually I only need sdl string operator[] but how
can I call it from the derived class. Someone any idea???


Deriving from string is a bad idea (and seemingly unnecessary in your
example, you "MyString" class should contain the string and then you
could do
reference operator[](size_type n) { return contained_string[n]); }

class MyString: public std::string
{
reference operator[](size_type n) {return std::string::operator[](n)}
// doesn't work
char& operator[](size_type n) {return std::string::operator[](n)}
// doesn't work
char& operator[](size_type n) {return *this.operator[](n)}
// doesn't work


You forgot the semicolons on all the return statements.

reference operator[](size_type) {
return static_cast<std::string*>(this)->operator[](n); }


This was just the extract that was relevant!! The class is more than
that little bit ;-)! And believe me if you have an old library and some
200.000 lines of code using that old crap its worthwile to write an
interface(even if you have to derive from the sdl String)! ;-)
Second! Yes I forgot the ;! Sorry was typing from memory!

So! I still need some help!!! Someone out there!!

DevH

Jul 19 '05 #4
John Harrison wrote:
>
> reference operator[](size_type) {
> return static_cast<std::string*>(this)->operator[](n); }

So! I still need some help!!! Someone out there!!


Have you tried Ron's suggestion? Looks good to me.

john


using a string as a member is not a question! Because of efficiency reasons!
If i write an interfacefunction for every std string function there is it
take too much time for me and costs too much cpu power!
all other operators work perfect but the compiler had a problem with the
parameter type of operator[];

Thanx for your solution but I wouldn't work!
The problem was that size_type is out of scope for the derived class
MyString. changing size_type to int did work out!
so this does works now:

reference operator[](int n) {return std::string::operator[](n)}

Thank you anyway!

But I have another prob! I more difficult one! the old class had a cast
operator char*()
Well! Yes some lunatic provided one! God knows why!
So! Maybe you know something to get around that!
casting c_str() is not a good idea because the c_str() returns only a copy
not the real representation!

Appreciate help! Cheers!!
DevH
Jul 19 '05 #5

But I have another prob! I more difficult one! the old class had a cast
operator char*()
Well! Yes some lunatic provided one! God knows why!
So! Maybe you know something to get around that!
casting c_str() is not a good idea because the c_str() returns only a copy
not the real representation!

But c_str() may return a copy, it doesn't have to. If you have an
implmentation of std::string which doesn't do reference counting and where
c_str() does return the real representation then maybe you could get away
with a cast.

Other than that I think you're stuck mate. Write your own class, don't base
it on std::string, or rewrite the string using parts of the library. That's
what I would do. Think about it, this guy has left you a maintenance
nightmare with his poorly thought out code, and you're replacing his poorly
thought out code with some more badly concieved code. making a nightmare for
someone else down the road. Try and make you're higher up aware of this and
tell them that the job needs doing properly.

You started this thread with 'I'm trying to clean up this old library'. At
the moment you are doing the opposite.
Appreciate help! Cheers!!
DevH


john
Jul 19 '05 #6

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

Similar topics

0
by: Anand | last post by:
class base: def __setattr__(self,attr,key,*unexpected): print "Base Class :",attr,key,unexpected,self.__dict__ self.__dict__ = key def __getattr__(self,attr,*unexpected): print "Base Class...
1
by: Steven T. Hatton | last post by:
I started working on something I had failed to understand several months ago. The original discussion on c.l.c++-m has this message ID: news://Message-ID:...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
6
by: Microsoft | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
6
by: John Glover | last post by:
I'm having a very strange problem with XML serialization. I'm writing web services which pass instances of various classes back and forth as parameters and return values of web methods. The...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
4
by: =?Utf-8?B?UmljaGFyZCBCeXNvdXRo?= | last post by:
Hi I'm trying to create a composite control to be used on a ToolStrip, consisting of a ToolStripComboBox and a ToolStripButton. It's going to be a control used to search for text in a grid...
7
by: Jim Langston | last post by:
This is something someone was asking in irc. I really don't need to do this right now, but may have to in the future. The following code is in error (marked). #include <iostream> #include...
2
by: Steve Richter | last post by:
KeyValuePair<string,stringhas a ToString method that returns the KeyValue pair seperated by a comma and enclosed in : Is this method used as a building block for serialization? The reason I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...

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.