Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > strange operator method

Reply
Thread Tools

strange operator method

 
 
Christopher
Guest
Posts: n/a
 
      11-30-2011
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?
 
Reply With Quote
 
 
 
 
Christopher
Guest
Posts: n/a
 
      11-30-2011
On Nov 30, 1:46*pm, bartek szurgot <(E-Mail Removed)> 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.?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-30-2011
On 11/30/2011 3:53 PM, Christopher wrote:
> On Nov 30, 1:46 pm, bartek szurgot<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
Member operators operator>>() and operator<<() Alex Vinokur C++ 3 03-20-2005 03:11 PM
operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous: Alex Vinokur C++ 4 11-26-2004 11:46 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments