By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.