Re: operator overloading

From: Robert W Hand (rwhand_at_NOSPAMoperamail.com)
Date: 03/29/04


Date: Mon, 29 Mar 2004 12:08:39 -0500

On Sat, 27 Mar 2004 21:29:54 +1100, "Rappy" <rapp13@iprimus.com.au>
wrote:

>having probs with addition operator overloading, error message tells me that
>the binary 'operator +' has two many parameters. how can this be? it is
>supposed to add two class operands.
>
>declaration in class:
> const SalesRegister operator +(const SalesRegister& lhs, const
>SalesRegister& rhs);
>
>definition in implementation file:
> const SalesRegister SalesRegister::operator+(const SalesRegister
>&param)

I assume that the first 'two' is really 'too'. :-)

This construction is a non-static member function, is it not? There
is an implicit object passed to the function. Remember the this
pointer? ;-)

Binary overloaded operators shall be implemented by a non-static
member function with exactly one parameter or by a non-member function
with two parameters. So x+y can be interpreted as x.operator +(y) or
operator +(x, y) depending on which form is declared.

Best wishes,

Bob



Relevant Pages

  • Re: where is the template instance?
    ... >>What compiles and runs? ... >>definition, an in class member function definition and a declaration of, ... header file with a class definition, an in class member function definition ... I can provide it; however people complain when you ...
    (alt.comp.lang.learn.c-cpp)
  • firefox error
    ... declaration of `PRAddrInfo' with no type ... nsHostResolver.h:209: error: ISO C++ forbids ... nsDNSService2.cpp: In member function `virtual ... gmake: Leaving directory ...
    (freebsd-questions)
  • Re: Attributed programming and injected code
    ... > What is the way to overwrite COM functions, like QueryInterface for ... > declaration to the object result in error "member function already ...
    (microsoft.public.vc.atl)
  • Re: Static Function Modifer
    ... William DePalo wrote: ... That pointer would conflict with the declaration of thread ... non-static member functions cannot be used a thread procs ... Only a non-member function or a static member function can be converted ...
    (microsoft.public.vc.language)
  • Re: My first thread... program-wise that is
    ... you need to declare it static in the header file. ... non-static member function, it has a hidden parameter representing the ... because it gave the declaration for a non-member or static ... Microsoft MVP - Visual C++ ...
    (microsoft.public.vc.mfc)