简体   繁体   中英

Why is Polymorphism is needed when we have the keyword “auto” in C++

Correct me if i'm wrong, but the keyword "auto" can solve all the problems that polymorphism was invented to solve.

instead of declaring of virtual function func of class A, when we have Class B inheriting from it, we can just say that the function func is receiving auto param, and it will know what case to trigger.

What do i miss?

No, auto cannot solve any of those problems.

auto is a compile-time utility to deduce the type of a thing from the code that's around it. It is more closely related to templates (indeed, the auto in the parameter list of a lambda is implemented by a template!).

It has no knowledge of runtime states and cannot be used to perform virtual dispatch.

These are completely different concepts and your best bet would be to re-study what each one means.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM