0

In my .h I have

const CResultats & operator = (const CResultats & desResultats);

when I write this :

CResultats CResultats :: operator = (const CResultats & desResultats) const
{
}

Why is it not working? I'm new to c++... sorry if it's stupid.

Sadboy
  • 1
  • 1
  • 2
    Read this: [Meaning of “const” last in a C++ method declaration?](http://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-c-method-declaration) – Csq Sep 26 '14 at 13:39

3 Answers3

1

Declare the operator as

CResultats & operator = (const CResultats & desResultats);

and define it as

CResultats & CResultats :: operator = (const CResultats & desResultats)
{
}

You may declare (and define) the operastor as

const CResultats & operator = (const CResultats & desResultats);

However semantically it is an invalid declaration.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • @Sadboy in that case change the return type of the operator to `const CResultats &`. Vlad's version is according to the assign operator's convention though (i.e. your definition is unconventional). – BeyelerStudios Sep 26 '14 at 13:46
  • @BeyelerStudios and what purpose would `const` assignment operator serve? It's not about convention, it's about making sense at all. – Bartek Banachewicz Sep 26 '14 at 13:47
  • @BartekBanachewicz you didn't read the part of "change the return type" or am I misreading your question? – BeyelerStudios Sep 26 '14 at 13:48
  • @BeyelerStudios you misread my question. The return type of assignment is absolutely irrelevant in what I was pointing out (and is a hack anyway, because assignment should be a statement, not an expression). – Bartek Banachewicz Sep 26 '14 at 13:49
  • @Sadboy What are you not allowed to change? For example the definition of the operator is simply invalid. – Vlad from Moscow Sep 26 '14 at 13:50
  • @BartekBanachewicz Ah I get it! In my statement I refer to Vlad's version of the assignment operator, not Sadboy's. Sadly, I cannot edit that statement anylonger. The full signature would look like this: `const CResultats& CResultats::operator=(const CResultats&)` – BeyelerStudios Sep 26 '14 at 13:52
1

This:

const CResultats & operator = (const CResultats & desResultats);

Is a declaration of a function which takes a CResultats by const-reference, and returns a CRFesultats by const-reference. The method itself is not const.

But this:

CResultats CResultats :: operator = (const CResultats & desResultats) const

Is a function definition for a const function which takes a CResultats by const-reference, and returns a CResultats by-value.

Those are two different things in two ways:

  1. You declare the function to return by const-reference, but you define it it return by value.
  2. You declare the function to be non-const, but you define it to be const.

If you want to definition to match the declaration, then you must change the definition to:

const CResultats & CResultats::operator = (const CResultats & desResultats)
{
  // ...
}
John Dibling
  • 99,718
  • 31
  • 186
  • 324
0

When you write "this", it's not "valid" because it doesn't fit the signature; it's different for the compiler

A member operator (function) having const at the end operates at const objects, as simple as that. In this case it doesn't make any sense, though, because the only sensible operation of this operator is modifying the object.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135