Why is virtual function not being called?

后端 未结 6 1922
挽巷
挽巷 2021-01-26 07:00
//GUITEXT
class guitext : public entity {
public:
    guitext(graphics *gfx, std::string _text, float _x, float _y, 
        float _size, float timeToLive);
    bool upd         


        
6条回答
  •  Happy的楠姐
    2021-01-26 07:13

    You should define entitys to contain pointers to entity. Slightly edited example derived from your code.

    #include "stdafx.h"
    #include 
    #include 
    
    class entity 
    {
    public:
        virtual void draw() { }
    };
    
    class guitext : public entity 
    {
    public:
        void draw()
        {
            printf("draw");
        }
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        std::vector entitys;
    
        guitext g;
    
        entitys.push_back(&g); 
    
        for(int i = 0; i < (int)entitys.size(); i++) 
        { 
            entitys[i]->draw(); 
        }
    
        return 0;
    }
    

提交回复
热议问题