help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: porting error I don't understand


From: Paul Pluzhnikov
Subject: Re: porting error I don't understand
Date: Thu, 12 May 2005 20:48:17 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

"mark" <m.somers@chem.leidenuniv.nl> writes:

[Message slightly re-ordered.]

> As far as I understand, the compiler is not able to find a correct
> operator= function, yet I think I've defined and implemented one.

No, you didn't.

The compiler is not allowed to bind temporaries to non-const
references, so your op=() is not good if right-hand side is a
temporary (returns from functions, e.g. ProductMatrix() are
temporary).

So, the correct operator=() signature should be:

    Matrix&  operator=( const Matrix& A );
  
> The reason why in operator= the A matrix is passed as a reference is
> because the operator= is used to "pass-through" memory from A to *this
> by copying a pointer. In A however, the pointer should be set to 0 (or
> NULL) in order to make sure that the calling of the destructor of A
> doesn't free the memory. So in short the operator= changes Matrix *this
> as well as Matrix A.

This does not necessarily mean that 'operator=()' can't take 
"const& Matrix" as a parameter. You could cast away constness,
e.g. with const_cast. Alternatively, use 'mutable' on the pointer
that you must assign in the "const Matrix &A" object.

Cheers,
-- 
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]