strange operator method

Discussion in 'C++' started by Christopher, Nov 30, 2011.

  1. Christopher

    Christopher Guest

    I am not sure how to get to the underlying string data in these
    objects:

    struct Buffer
    {
    boost::shared_array<char> value_;
    unsigned int used_;
    mutable bool sealed_;
    };

    struct BufferWrapper
    {
    boost::shared_ptr<Buffer> buffer;

    // What is this and how to use it?
    operator boost::asio::const_buffer() const;
    {
    return boost::asio::buffer(buffer->value_.get(), buffer->used_ *
    sizeof(char));
    }
    };


    I am used to seeing <return type> operator <some operator> (paramters)
    { <body> }
    How can I get to the string data?
     
    Christopher, Nov 30, 2011
    #1
    1. Advertising

  2. Christopher

    Christopher Guest

    On Nov 30, 1:46 pm, bartek szurgot <> wrote:
    > it is a conversion operator. consider following code:
    >
    > struct X
    > {
    >   //operator int(void) { return 42; }
    >
    > };
    >
    > int main(void)
    > {
    >   X   x;
    >   int i=x;
    >   return i;
    >
    > }
    >
    > it won't compile, until you uncomment operator for automatic X->int
    > conversion. then return value of the program will be 42, as expected.


    If a "conversion" operator is supplied for a class or struct, is its
    usage umm... always valid and implicit?
    i.e Can I pass x to a function that expects an int?
    Can I stream it into a ostringstream?
    etc.?
     
    Christopher, Nov 30, 2011
    #2
    1. Advertising

  3. On 11/30/2011 3:53 PM, Christopher wrote:
    > On Nov 30, 1:46 pm, bartek szurgot<> wrote:
    >> it is a conversion operator. consider following code:
    >>
    >> struct X
    >> {
    >> //operator int(void) { return 42; }
    >>
    >> };
    >>
    >> int main(void)
    >> {
    >> X x;
    >> int i=x;
    >> return i;
    >>
    >> }
    >>
    >> it won't compile, until you uncomment operator for automatic X->int
    >> conversion. then return value of the program will be 42, as expected.

    >
    > If a "conversion" operator is supplied for a class or struct, is its
    > usage umm... always valid and implicit?


    Not sure what you mean by "valid" here.

    > i.e Can I pass x to a function that expects an int?


    Yes.

    > Can I stream it into a ostringstream?
    > etc.?


    You can try.

    The point is that by adding a conversion operator you allowing an
    implicit conversion from that type to the return type of that operator.
    Whether it's going to be used depends on the context.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 30, 2011
    #3
    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. Dmitriy Iassenev

    to guru : strange C++ operator behaviour

    Dmitriy Iassenev, Oct 2, 2003, in forum: C++
    Replies:
    35
    Views:
    834
  2. Harvey Twyman
    Replies:
    8
    Views:
    566
    August Derleth
    Oct 25, 2003
  3. paulo

    Strange operands to conditional operator

    paulo, Nov 14, 2006, in forum: C Programming
    Replies:
    5
    Views:
    314
    Keith Thompson
    Nov 14, 2006
  4. vj
    Replies:
    11
    Views:
    565
  5. Kyung won Cheon
    Replies:
    0
    Views:
    207
    Kyung won Cheon
    Nov 21, 2008
Loading...

Share This Page