In C++, suppose I have a function dummy(foo& f)
that does some computation on the public members of f
. Now there is a class bar
that publicly inherits from foo
, and b
is an object of bar
. Can I call dummy
on b
? Or I need to redefine dummy(bar& b)
? What is the best practice here? Note that the scenario is a bit different from the scenario where virtual function and runtime polymorphism can be applied.
Asked
Active
Viewed 30 times
0

Zitao Wang
- 101
- 2
- 2
-
Did you try it? Did it work or not? – eerorika Jan 24 '20 at 05:28
-
@eerorika I tried it and it worked, but I still want to understand why. – Zitao Wang Jan 24 '20 at 05:36
-
@Dai, this is exactly what I'm looking for. Thanks for the reference! – Zitao Wang Jan 24 '20 at 05:36
-
Can you give a small example of what you tried? – Jan 24 '20 at 05:37
-
@Chipster my question is answered in https://stackoverflow.com/questions/9285627/is-it-possible-to-pass-derived-classes-by-reference-to-a-function-taking-base-cl. You can refer there for concrete examples. – Zitao Wang Jan 24 '20 at 05:38