Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > operator << and conversion operator

Reply
Thread Tools

operator << and conversion operator

 
 
Ian Collins
Guest
Posts: n/a
 
      10-28-2010
On 10/28/10 05:18 PM, Ian Collins wrote:
> On 10/28/10 05:10 PM, cronusf wrote:
>>
>> std:stream& operator<<(std:stream& os, Complex& c)
>> {
>> os<< "("<< c.real<< ", "<< c.imag<< ")"<< std::endl;
>>
>> return os;
>> }
>>
>> int main()
>> {
>> Complex c1(1.0f, 2.0f);
>> Complex c2(1.0f, 2.0f);
>>
>> std::cout<< c1 + c2<< std::endl;
>> }
>>
>> Output: (2, 4)

>
> That shouldn't compile. There isn't a suitable operator << for Complex.


Which probably means that compiler doesn't enforce the can't bind a
temporary to a non const reference rule. So what you see is compiler,
rather than language specific.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      10-28-2010
* Ian Collins, on 28.10.2010 06:30:
> On 10/28/10 05:18 PM, Ian Collins wrote:
>> On 10/28/10 05:10 PM, cronusf wrote:
>>>
>>> std:stream& operator<<(std:stream& os, Complex& c)
>>> {
>>> os<< "("<< c.real<< ", "<< c.imag<< ")"<< std::endl;
>>>
>>> return os;
>>> }
>>>
>>> int main()
>>> {
>>> Complex c1(1.0f, 2.0f);
>>> Complex c2(1.0f, 2.0f);
>>>
>>> std::cout<< c1 + c2<< std::endl;
>>> }
>>>
>>> Output: (2, 4)

>>
>> That shouldn't compile. There isn't a suitable operator << for Complex.

>
> Which probably means that compiler doesn't enforce the can't bind a temporary to
> a non const reference rule. So what you see is compiler, rather than language
> specific.


Yes, "cronusfh" is using Visual C++ 8.

Presumably he's ignoring the warning about non-standard language extension being
used.

Cheers,

- Alf

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      10-28-2010
On 27 Okt., 03:36, cronusf wrote:
> Suppose I have a class Complex and I overload operator double()
> to get the real part.


I would consider this a /misuse/ of operator overloading. Try to
avoid "lossy" user-defined implicit conversions. Here, the resulting
double value won't properly represent the value of the original
complex object in general. Make the user explicitly say what he/she
intends to do to avoid any accidental data losses.

Cheers!
SG
 
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
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion xhtml champs Python 0 06-21-2011 11:59 AM
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion PSD to XHTML Conversion Services and PSD to HTML CSS Conversion Services, PSD to Joomla, Drupal, Wor VHDL 0 04-25-2011 06:43 AM
conversion operator and conversion ctor subramanian100in@yahoo.com, India C++ 2 09-15-2009 12:46 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
<complex> : no match for 'operator*' // conversion operator double() Arvid Requate C++ 2 06-23-2006 10:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57