cast operator

Discussion in 'C++' started by Ali R., Feb 17, 2004.

  1. Ali R.

    Ali R. Guest

    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
    Ali R., Feb 17, 2004
    #1
    1. Advertising

  2. 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." <> wrote in message
    news:11vYb.207$...
    > 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, Feb 17, 2004
    #2
    1. Advertising

  3. Ali R.

    Ron Natalie Guest

    "Ali R." <> wrote in message news:11vYb.207$...
    > Hi guys,
    >
    > I have a string class, which is a wrapper for a char *
    > with a few operators, like +


    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*.
    Ron Natalie, Feb 17, 2004
    #3
  4. "Ali R." <> wrote in message
    news:11vYb.207$...
    > 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);


    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*);


    > <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;
    > }


    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*.

    > What I get is an error on the cout line saying
    >
    > Thanks Ali


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

    Jonathan
    Jonathan Turkanis, Feb 17, 2004
    #4
  5. Ali R.

    Ali R. Guest

    "Jonathan Turkanis" <> wrote in message
    news:c0tvfv$1c1osr$-berlin.de...
    > "Ali R." <> wrote in message
    > news:11vYb.207$...
    > > 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);

    >
    > 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*);
    >
    >
    > > <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;
    > > }

    >
    > 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*.
    >
    > > What I get is an error on the cout line saying
    > >
    > > Thanks Ali

    >

    LOL, I fogot to cut and paste the error line! And I can't type today

    > I wish my compiler knew my name, and was so polite!
    >
    > Jonathan
    >
    >
    >
    Ali R., Feb 17, 2004
    #5
  6. Ali R.

    Ali R. Guest

    Thanks Guys,

    Ali

    "Ali R." <> wrote in message
    news:11vYb.207$...
    > 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
    >
    >
    Ali R., Feb 17, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Showjumper
    Replies:
    0
    Views:
    316
    Showjumper
    Jan 17, 2004
  2. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,072
    Dan Pop
    Feb 10, 2004
  3. EvilRix
    Replies:
    8
    Views:
    635
    Martin Dickopp
    Feb 14, 2004
  4. John Goche
    Replies:
    2
    Views:
    342
    Frederick Gotham
    Sep 4, 2006
  5. Pavel
    Replies:
    7
    Views:
    524
    Pavel
    Sep 19, 2010
Loading...

Share This Page