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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • K.S.

    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::op erator[](n)}
    // doesn't work
    char& operator[](size_type n) {return std::string::op erator[](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
  • Ron Natalie

    #2
    Re: C++ Building derived class from sdl string!HELP!


    "K.S." <schnass@iue.tu wien.ac.at> wrote in message news:3f428704$0 $4906$91cee783@ newsreader01.hi ghway.telekom.a t...[color=blue]
    > 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???[/color]

    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_strin g[n]); }[color=blue]
    >
    > class MyString: public std::string
    > {
    > reference operator[](size_type n) {return std::string::op erator[](n)}
    > // doesn't work
    > char& operator[](size_type n) {return std::string::op erator[](n)}
    > // doesn't work
    > char& operator[](size_type n) {return *this.operator[](n)}
    > // doesn't work[/color]

    You forgot the semicolons on all the return statements.

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


    Comment

    • K.S.

      #3
      Re: C++ Building derived class from sdl string!HELP!

      Ron Natalie wrote:
      [color=blue]
      >
      > "K.S." <schnass@iue.tu wien.ac.at> wrote in message
      > news:3f428704$0 $4906$91cee783@ newsreader01.hi ghway.telekom.a t...[color=green]
      >> 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???[/color]
      >
      > 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_strin g[n]); }[color=green]
      >>
      >> class MyString: public std::string
      >> {
      >> reference operator[](size_type n) {return std::string::op erator[](n)}
      >> // doesn't work
      >> char& operator[](size_type n) {return std::string::op erator[](n)}
      >> // doesn't work
      >> char& operator[](size_type n) {return *this.operator[](n)}
      >> // doesn't work[/color]
      >
      > You forgot the semicolons on all the return statements.
      >
      > reference operator[](size_type) {
      > return static_cast<std ::string*>(this )->operator[](n); }[/color]

      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

      Comment

      • K.S.

        #4
        Re: C++ Building derived class from sdl string!HELP!

        Ron Natalie wrote:
        [color=blue]
        >
        > "K.S." <schnass@iue.tu wien.ac.at> wrote in message
        > news:3f428704$0 $4906$91cee783@ newsreader01.hi ghway.telekom.a t...[color=green]
        >> 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???[/color]
        >
        > 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_strin g[n]); }[color=green]
        >>
        >> class MyString: public std::string
        >> {
        >> reference operator[](size_type n) {return std::string::op erator[](n)}
        >> // doesn't work
        >> char& operator[](size_type n) {return std::string::op erator[](n)}
        >> // doesn't work
        >> char& operator[](size_type n) {return *this.operator[](n)}
        >> // doesn't work[/color]
        >
        > You forgot the semicolons on all the return statements.
        >
        > reference operator[](size_type) {
        > return static_cast<std ::string*>(this )->operator[](n); }[/color]

        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

        Comment

        • K.S.

          #5
          Re: C++ Building derived class from sdl string!HELP!

          John Harrison wrote:
          [color=blue][color=green][color=darkred]
          >> >
          >> > reference operator[](size_type) {
          >> > return static_cast<std ::string*>(this )->operator[](n); }[/color]
          >>
          >>
          >> So! I still need some help!!! Someone out there!![/color]
          >
          > Have you tried Ron's suggestion? Looks good to me.
          >
          > john[/color]

          using a string as a member is not a question! Because of efficiency reasons!
          If i write an interfacefuncti on 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::op erator[](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

          Comment

          • John Harrison

            #6
            Re: C++ Building derived class from sdl string!HELP!

            [color=blue]
            >
            > 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!
            >[/color]

            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.
            [color=blue]
            > Appreciate help! Cheers!!
            > DevH[/color]

            john


            Comment

            Working...