Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > += operation

Reply
Thread Tools

+= operation

 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-15-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> On Jun 15, 11:05 am, Sze <(E-Mail Removed)> wrote:
>> > This time my code is:

>>
>> > ComplexNum& ComplexNum:perator += (const ComplexNum& rhs)
>> > {
>> > if(&rhs != this)
>> > {
>> > real+= rhs.real;
>> > imaginary += rhs.imaginary;
>> > }
>> > return *this;
>> > }

>>
>> > is it correct?

>>
>> Why shouldn`t you allow addition to itself?
>> I think it does make sense to allow it, this is another case as with
>> the assignment operator.

>
> Do you mean this:
> ComplexNum& ComplexNum:perator += (const ComplexNum& rhs)
> {
> if(&rhs != this)
> {
> real= real+rhs.real;
> imaginary = imaginary+ rhs.imaginary;
> }
> return *this;
> }


No.

You made the first test case work. Now, also do this one (without breaking
the first):

int main() {
ComplexNum a( 1, 2 );
a += a;
assert( a.real() == 2 );
assert( a.imag() == 4 );
cout << "OK\n";
}



Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
foolsmart2005@gmail.com
Guest
Posts: n/a
 
      06-15-2008
On Jun 15, 11:45 am, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Jun 15, 11:05 am, Sze <(E-Mail Removed)> wrote:
> >> > This time my code is:

>
> >> > ComplexNum& ComplexNum:perator += (const ComplexNum& rhs)
> >> > {
> >> > if(&rhs != this)
> >> > {
> >> > real+= rhs.real;
> >> > imaginary += rhs.imaginary;
> >> > }
> >> > return *this;
> >> > }

>
> >> > is it correct?

>
> >> Why shouldn`t you allow addition to itself?
> >> I think it does make sense to allow it, this is another case as with
> >> the assignment operator.

>
> > Do you mean this:
> > ComplexNum& ComplexNum:perator += (const ComplexNum& rhs)
> > {
> > if(&rhs != this)
> > {
> > real= real+rhs.real;
> > imaginary = imaginary+ rhs.imaginary;
> > }
> > return *this;
> > }

>
> No.
>
> You made the first test case work. Now, also do this one (without breaking
> the first):
>
> int main() {
> ComplexNum a( 1, 2 );
> a += a;
> assert( a.real() == 2 );
> assert( a.imag() == 4 );
> cout << "OK\n";
>
> }
>
> Best
>
> Kai-Uwe Bux


for a +=a; this one, I have no idea. How can I do this?
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-15-2008
(E-Mail Removed) wrote:

> On Jun 15, 11:45 am, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>> > On Jun 15, 11:05 am, Sze <(E-Mail Removed)> wrote:
>> >> > This time my code is:

>>
>> >> > ComplexNum& ComplexNum:perator += (const ComplexNum& rhs)
>> >> > {
>> >> > if(&rhs != this)
>> >> > {
>> >> > real+= rhs.real;
>> >> > imaginary += rhs.imaginary;
>> >> > }
>> >> > return *this;
>> >> > }

>>
>> >> > is it correct?

>>
>> >> Why shouldn`t you allow addition to itself?
>> >> I think it does make sense to allow it, this is another case as with
>> >> the assignment operator.

>>
>> > Do you mean this:
>> > ComplexNum& ComplexNum:perator += (const ComplexNum& rhs)
>> > {
>> > if(&rhs != this)
>> > {
>> > real= real+rhs.real;
>> > imaginary = imaginary+ rhs.imaginary;
>> > }
>> > return *this;
>> > }

>>
>> No.
>>
>> You made the first test case work. Now, also do this one (without
>> breaking the first):
>>
>> int main() {
>> ComplexNum a( 1, 2 );
>> a += a;
>> assert( a.real() == 2 );
>> assert( a.imag() == 4 );
>> cout << "OK\n";
>>
>> }
>>
>> Best
>>
>> Kai-Uwe Bux

>
> for a +=a; this one, I have no idea. How can I do this?


Hint: ask yourself why you insist on the if(&rhs != this) check.


Best

Kai-Uwe Bux
 
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
Boolean operation and arithmetic operation Buzz Lightyear C++ 10 08-12-2009 01:27 PM
I/O operation, file operation behaviou raan C++ 2 08-16-2007 07:13 PM
Does bit operation always work more efficiently than math operation? david ullua C Programming 13 03-01-2006 11:02 PM
DWL-G650 operation with (and without) WPA2 Fred Marshall Wireless Networking 6 05-08-2005 02:14 AM
my AMD compaq laptop frooze about 4 minutes of operation =?Utf-8?B?SGFycnkgTGV1bmc=?= Wireless Networking 0 08-17-2004 04:15 AM



Advertisments