dimanche 14 juin 2020

Call derived class method from base class instance without casting

There is another way to call derived class method from a base pointer object without casting?

class Base
{
};

class Derived : public Base
{
    public:
    void set() {printf("bbbbbbbbbbbb"); }

};

int main()
{
  Base* pbBase;

  pbBase = new Derived();

  //pbBase->set(); // Compilation error.
  (Derived*)pbBase->set(); // Working.
}

Aucun commentaire:

Enregistrer un commentaire