cast operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ali R.

    cast operator

    Hi guys,

    I have a string class, which is a wrapper for a char *
    with a few operators, like +
    What I am trying to do is reduce the number of overloaded operators
    so for insteance

    class String
    {
    friend String operator + (const String &Left,const char *Right);
    friend String operator + (const char *Left,const String &Right);

    //It would be nice if I could avoid this
    friend String operator + (const String &Left,const String &Right);

    private:
    char *m_Buffer;
    }

    It would be nice if I could avoid this
    friend String operator + (const String &Left,const String &Right);
    since String can be easily substituded with a char *

    a line like this

    String Str1("Test");
    String Str2("Test");

    String Res = Str1 + Str2;

    should be able to call this version, if the compiler could automatically
    cast the string object into a char *
    friend String operator + (const String &Left,const char *Right);

    is there a way I can define a cast operator for this class, like
    class String
    {
    public
    operator const char *() const { return m_Buffer; }
    private:
    char *m_Buffer;
    }

    so that I can do things like the + above, it would work?

    I know I have seen this done somewhere, but I can figure out how they did
    it.
    What I get is an error on the cout line saying

    Thanks Ali


  • Carl Ribbegaardh

    #2
    Re: cast operator

    How about writing a constructor that takes a char[] ?
    Then the char[] would be converted into a String, and all you need would be
    an operator + for 2 Strings. :)

    ....i hope.

    /Carl

    "Ali R." <nospam@nospam. com> wrote in message
    news:11vYb.207$ xD6.30181429@ne wssvr11.news.pr odigy.com...[color=blue]
    > Hi guys,
    >
    > I have a string class, which is a wrapper for a char *
    > with a few operators, like +
    > What I am trying to do is reduce the number of overloaded operators
    > so for insteance
    >
    > class String
    > {
    > friend String operator + (const String &Left,const char *Right);
    > friend String operator + (const char *Left,const String &Right);
    >
    > //It would be nice if I could avoid this
    > friend String operator + (const String &Left,const String &Right);
    >
    > private:
    > char *m_Buffer;
    > }
    >
    > It would be nice if I could avoid this
    > friend String operator + (const String &Left,const String &Right);
    > since String can be easily substituded with a char *
    >
    > a line like this
    >
    > String Str1("Test");
    > String Str2("Test");
    >
    > String Res = Str1 + Str2;
    >
    > should be able to call this version, if the compiler could automatically
    > cast the string object into a char *
    > friend String operator + (const String &Left,const char *Right);
    >
    > is there a way I can define a cast operator for this class, like
    > class String
    > {
    > public
    > operator const char *() const { return m_Buffer; }
    > private:
    > char *m_Buffer;
    > }
    >
    > so that I can do things like the + above, it would work?
    >
    > I know I have seen this done somewhere, but I can figure out how they did
    > it.
    > What I get is an error on the cout line saying
    >
    > Thanks Ali
    >
    >[/color]


    Comment

    • Ron Natalie

      #3
      Re: cast operator


      "Ali R." <nospam@nospam. com> wrote in message news:11vYb.207$ xD6.30181429@ne wssvr11.news.pr odigy.com...[color=blue]
      > Hi guys,
      >
      > I have a string class, which is a wrapper for a char *
      > with a few operators, like +[/color]

      Sounds like what you want is a converting constructor, like the std::string
      class has.

      class String {
      public:
      String(const char*);
      String();
      ....
      };

      Then you pretty much give a const char* to anything expecting a String and it will
      be converted.

      The operator char*() you defined does the opposite (and is probably ill-advised).
      It converts you String class to a const char*.

      Comment

      • Jonathan Turkanis

        #4
        Re: cast operator

        "Ali R." <nospam@nospam. com> wrote in message
        news:11vYb.207$ xD6.30181429@ne wssvr11.news.pr odigy.com...[color=blue]
        > Hi guys,
        >
        > I have a string class, which is a wrapper for a char *
        > with a few operators, like +
        > What I am trying to do is reduce the number of overloaded operators
        > so for insteance
        >
        > class String
        > {
        > friend String operator + (const String &Left,const char *Right);[/color]

        This is the one you can get rid of easily, by providing a non-explicit
        String constructor taking a const char*. That's how the standard
        library does it:

        String(const char*);

        [color=blue]
        > <snip>
        > is there a way I can define a cast operator for this class, like
        > class String
        > {
        > public
        > operator const char *() const { return m_Buffer; }
        > private:
        > char *m_Buffer;
        > }[/color]

        This should work. There are some well-known problems that can arise
        with conversion operators, which you can find discussed in books like
        Effective C++, Modern C++ Design, ... (and maybe also the FAQ for this
        group.) This is why the standard library uses converting constructors
        but explicit member functions (c_str() and data()) for converting to
        const char*.
        [color=blue]
        > What I get is an error on the cout line saying
        >
        > Thanks Ali[/color]

        I wish my compiler knew my name, and was so polite!

        Jonathan



        Comment

        • Ali R.

          #5
          Re: cast operator

          "Jonathan Turkanis" <technews@kanga roologic.com> wrote in message
          news:c0tvfv$1c1 osr$1@ID-216073.news.uni-berlin.de...[color=blue]
          > "Ali R." <nospam@nospam. com> wrote in message
          > news:11vYb.207$ xD6.30181429@ne wssvr11.news.pr odigy.com...[color=green]
          > > Hi guys,
          > >
          > > I have a string class, which is a wrapper for a char *
          > > with a few operators, like +
          > > What I am trying to do is reduce the number of overloaded operators
          > > so for insteance
          > >
          > > class String
          > > {
          > > friend String operator + (const String &Left,const char *Right);[/color]
          >
          > This is the one you can get rid of easily, by providing a non-explicit
          > String constructor taking a const char*. That's how the standard
          > library does it:
          >
          > String(const char*);
          >
          >[color=green]
          > > <snip>
          > > is there a way I can define a cast operator for this class, like
          > > class String
          > > {
          > > public
          > > operator const char *() const { return m_Buffer; }
          > > private:
          > > char *m_Buffer;
          > > }[/color]
          >
          > This should work. There are some well-known problems that can arise
          > with conversion operators, which you can find discussed in books like
          > Effective C++, Modern C++ Design, ... (and maybe also the FAQ for this
          > group.) This is why the standard library uses converting constructors
          > but explicit member functions (c_str() and data()) for converting to
          > const char*.
          >[color=green]
          > > What I get is an error on the cout line saying
          > >
          > > Thanks Ali[/color]
          >[/color]
          LOL, I fogot to cut and paste the error line! And I can't type today
          [color=blue]
          > I wish my compiler knew my name, and was so polite!
          >
          > Jonathan
          >
          >
          >[/color]


          Comment

          • Ali R.

            #6
            Re: cast operator

            Thanks Guys,

            Ali

            "Ali R." <nospam@nospam. com> wrote in message
            news:11vYb.207$ xD6.30181429@ne wssvr11.news.pr odigy.com...[color=blue]
            > Hi guys,
            >
            > I have a string class, which is a wrapper for a char *
            > with a few operators, like +
            > What I am trying to do is reduce the number of overloaded operators
            > so for insteance
            >
            > class String
            > {
            > friend String operator + (const String &Left,const char *Right);
            > friend String operator + (const char *Left,const String &Right);
            >
            > //It would be nice if I could avoid this
            > friend String operator + (const String &Left,const String &Right);
            >
            > private:
            > char *m_Buffer;
            > }
            >
            > It would be nice if I could avoid this
            > friend String operator + (const String &Left,const String &Right);
            > since String can be easily substituded with a char *
            >
            > a line like this
            >
            > String Str1("Test");
            > String Str2("Test");
            >
            > String Res = Str1 + Str2;
            >
            > should be able to call this version, if the compiler could automatically
            > cast the string object into a char *
            > friend String operator + (const String &Left,const char *Right);
            >
            > is there a way I can define a cast operator for this class, like
            > class String
            > {
            > public
            > operator const char *() const { return m_Buffer; }
            > private:
            > char *m_Buffer;
            > }
            >
            > so that I can do things like the + above, it would work?
            >
            > I know I have seen this done somewhere, but I can figure out how they did
            > it.
            > What I get is an error on the cout line saying
            >
            > Thanks Ali
            >
            >[/color]


            Comment

            Working...