Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Problems wi...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 9288 of 10096
Post > Topic >>

Re: Problems with + operator, trying to sum multiple objects

by Bart van Ingen Schenau <bart@[EMAIL PROTECTED] > Feb 16, 2008 at 03:16 PM

david wrote:

> Hi, first of all I am going to past a several parts of code.
> 
> That is my class for now:
> class Aibe {
>      public:
>          Aibe();                                 // veikia
>          Aibe(int arr[], int length);            // veikia
>          Aibe(int item, ...);                    // neveikia
>          ~Aibe();                                // veikia
> 
>          bool issubset(Aibe &other);
>          void toArray(int **arr);                // veikia
>          string toString();                      // veikia
>          int length();                           // veikia
> 
>          ostream & operator << (Aibe &other);    // veikia
>          Aibe & operator = (Aibe &two);            // pending
>          Aibe operator + (Aibe &two);            // veikia ?
>          Aibe operator - (Aibe &other);          // pending
>          Aibe operator * (const Aibe &other);    // pending

The problem is with the signatures of these operators.
operator+, operator- and operator* all return a tem****ary object, and
C++ does not allow such a tem****ary to be passed to another
function/operator through a non-const reference.
The fix is quite easy, as none of the operators modifies it right-hand
side, you can just pass the parameter as a const reference.

Additionally, as operator+, operator- and operator* do not modify the
object they are invoked on, they should be made const member-functions
(or ideally, taken out of the class as non-member operators).
   Aibe & operator = (const Aibe &two);
   Aibe operator + (const Aibe &two) const;
   Aibe operator - (const Aibe &other) const;
   Aibe operator * (const Aibe &other) const;

Bart v Ingen Schenau
-- 
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.para****ft.com/c++-faq-lite/

      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 1 Posts in Topic:
Re: Problems with + operator, trying to sum multiple objects
Bart van Ingen Schenau &l  2008-02-16 15:16:32 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 8:28:24 CDT 2008.