V
vsgdp
Say you have 3 child classes A, B, C deriving from a parent class P
with pure virtual function f.
Suppose A::f(x1); // A::f depends only on x1
Suppose B::f(x1, x2); // B::f depends on x1 and x2
Suppose C::f(x1, x2, x3); // C::f depends on x1, x2, x3
Is it bad form to define P::f(x1, x2, x3), and then A::f and B::f just
ignore the parameters it does not need?
In some sense this could be justified; you could say that for A::f, x2
and x3 are just "zero" or some identity value that doesn't actually
affect the output.
Any thoughts on this?
I think it is okay occasionally if not abused often, but I would like
the opinion of the C++ programming community.
For those that want some context, basically I have a Light class and I
derive:
DirectionalLight, PointLight, SpotLight, AreaLight. The area light
implementation requires some special data the others do not; this data
also varies over time so I can't just set it at construction time. I
can't set the area light's specific data at runtime either without
knowing its specific type (defeats polymorphism). So I am thinking
the best solution would be to just pass the data to the pure virtual
function, even though only area lights will use the data.
with pure virtual function f.
Suppose A::f(x1); // A::f depends only on x1
Suppose B::f(x1, x2); // B::f depends on x1 and x2
Suppose C::f(x1, x2, x3); // C::f depends on x1, x2, x3
Is it bad form to define P::f(x1, x2, x3), and then A::f and B::f just
ignore the parameters it does not need?
In some sense this could be justified; you could say that for A::f, x2
and x3 are just "zero" or some identity value that doesn't actually
affect the output.
Any thoughts on this?
I think it is okay occasionally if not abused often, but I would like
the opinion of the C++ programming community.
For those that want some context, basically I have a Light class and I
derive:
DirectionalLight, PointLight, SpotLight, AreaLight. The area light
implementation requires some special data the others do not; this data
also varies over time so I can't just set it at construction time. I
can't set the area light's specific data at runtime either without
knowing its specific type (defeats polymorphism). So I am thinking
the best solution would be to just pass the data to the pure virtual
function, even though only area lights will use the data.