Why doesn't polymorphism work without pointers/references?

前端 未结 6 1398
萌比男神i
萌比男神i 2020-11-22 03:09

I did find some questions already on SO with similar title- but when I read the answers they were focussing on different parts of the question which were really specific (e.

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 04:03

    You need pointers or reference because for the kind of polymorphism you are interested in (*), you need that the dynamic type could be different from the static type, in other words that the true type of the object is different than the declared type. In C++ that happens only with pointers or references.


    (*) Genericity, the type of polymorphism provided by templates, doesn't need pointers nor references.

提交回复
热议问题