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

K

K.S.

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::eek:perator[](n)}
// doesn't work
char& operator[](size_type n) {return std::string::eek:perator[](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
 
R

Ron Natalie

K.S. said:
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::eek:perator[](n)}
// doesn't work
char& operator[](size_type n) {return std::string::eek:perator[](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); }
 
K

K.S.

Ron said:
K.S. said:
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::eek:perator[](n)}
// doesn't work
char& operator[](size_type n) {return std::string::eek:perator[](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
 
K

K.S.

Ron said:
K.S. said:
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::eek:perator[](n)}
// doesn't work
char& operator[](size_type n) {return std::string::eek:perator[](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
 
K

K.S.

John said:
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::eek:perator[](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
 
J

John Harrison

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top