mardi 14 avril 2020

Altering virtual functions to add new parameters

I have a base class A (which is never instantiated in its base form) that is the parent to many many child classes. A has a function that every child class inherits, called func. Each child class has a different implementation of func but the input and output of each is the same.

in A

virtual void func();

and in each child class

void func();

While maintaining this code I realized i actually want to pass a parameter to this function, like so

virtual void func(float foo);

I then, apparently naively, assumed each class inheriting this function would then be able to take and use this parameter but it seems I am now going to have to go through each and every child class and add float foo to their implementation of the definition of func(), adding void func(float foo); for each of them. Isn't this kind of duplicated alters exactly what OOP is supposed to help avoid? I can access base class members without the need to duplicate their declarations. My question is am I misusing virtual functions, is there a way to get the behavior I want or is this just something that is a pain to change?

Aucun commentaire:

Enregistrer un commentaire