strange operator method

C

Christopher

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?
 
C

Christopher

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.?
 
V

Victor Bazarov

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top