1

I have two classes, and I want to surdefine the operator = .

class Composant {

string description;

...

virtual const Composant& operator=(const Composant &c)
{
    description = c.description;
    return *this;
}

}

And the other one inherited :

class Ecran : Composant {

int format, pitch;
double taille;

...

const Ecran& operator=(const Ecran &e)
{
    format = e.format;
    taille = e.taille;
    pitch = e.pitch;

    //traiter composant
    Composant::operator=(e);
}

Example of the code in the main :

Composant *p=new Ecran(.....);
Composant *t=new Ecran(.....);
(*p)=*t;

Is this the correct way to do it ?

I assume that we MUST put the surdefinition of the operator = in virtual, everytime we have this type of inheritance then ? same for the operator == and so on ...

mpromonet
  • 11,326
  • 43
  • 62
  • 91
Mxsky
  • 749
  • 9
  • 27
  • `operator=(const Composant &c)` and `operator=(const Ecran &e)` are completely different methods. Note that you are changing the parameter type, creating an overloaded operator. The latter is not overriding the former. – dhke Oct 03 '15 at 10:10
  • @dhke Member functions in different classes don't overload. – curiousguy Oct 03 '15 at 10:24
  • `Composant *p=new Ecran` will not compile. Try it! – curiousguy Oct 03 '15 at 10:27
  • @curiousguy What makes you think I cannot add another method with the same name as a method in the derived class that only differs in the parameters (which my nomenclature still calls *overloading* because I'm re-using the name)? But nitpicking aside, the thing is `Ecran` has two different `operator=()`, not a single virtual one. – dhke Oct 03 '15 at 10:39
  • @curiousguy That's because there's no `:public Composant` in the inheritance list. But I think it's more or less clear from the question that this is a mistake. – dhke Oct 03 '15 at 10:40
  • @curiousguy Since when do data members have parameters? *Function* overloading specifically refers to the technique of re-using a function name so that different versions are used in different contexts. In this case, we have function overloading based on parameter type. If `Base` has `doit(int)` and `Derived` has `doit(long)` the parameter type decides which one get's called. Hence `doit()` is overloaded for all instances of `Derived`. What else do you call that, because it's clearly not polymorphism? – dhke Oct 03 '15 at 11:04
  • @dhke "_Function overloading specifically refers to the technique_" no, overloading means that more than one function of a given name can be declared in one scope "_If Base has doit(int) and Derived has doit(long) the parameter type decides which one get's called_" no, the type of the object does – curiousguy Oct 03 '15 at 11:11
  • @curiousguy Nah, crap, I forgot it's C++ *sigh* You need a `using Base::doit` to get the behaviour I would expected (and think sane). Sorry for the noise. It's still overloading, mind you ;-) – dhke Oct 03 '15 at 11:24
  • @dhke "_It's still overloading,_" not according to the standard – curiousguy Oct 03 '15 at 11:27

2 Answers2

1

Is this the correct way to do it?

No virtual dispatch is going in:

(*p)=*t;

You are just calling Composant::operator=. Whether this is the correct way to do it or not depends on what you are trying to achieve.

Defining a virtual operator= is possible but not recommended.

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • How does Composant know how to use the operator = of Ecran if we put nothing in virtual ? See, i used pointers of Composant ... – Mxsky Oct 03 '15 at 10:02
1
const Ecran& operator=(const Ecran &e)
{
    format = e.format;
    taille = e.taille;
    pitch = e.pitch;

    //traiter composant
    Composant::operator=(e);
}

must return a value

return *this;

(*p)=*t; calls operator= of Composant class not Ecran class since this operates on Composant. You can define

virtual const Composant& operator=(const Composant &c);

in Ecran class and this would be called, however it will not have access to the member of Ecran in the right hand of the operator.

Lorenzo Belli
  • 1,767
  • 4
  • 25
  • 46