Showing posts with label virtual. Show all posts
Showing posts with label virtual. Show all posts

Friday, April 6, 2012

virtual method overload with non virtual


#include <iostream>
using namespace std;


struct A{
  virtual void f(){
    cout<<"a"<<endl;
  }
};

struct B:public A{
  void f(){
    cout<<"b"<<endl;
  }
};


struct C:public B{
  void f(){
    cout<<"c"<<endl;
  }
};


int main(){
  C c;
  A* a=&c;
  B* b=&c;
  a->f();
  b->f();

}

====out====
c
c

Wednesday, March 28, 2012

virtual destructor


#include <iostream>
struct A{
  virtual void f()=0;
};


struct B:public A{
  void f(){}
  ~B(){
    std::cout<<"~B"<<std::endl;
  }
};


int main(){
  A* a=new B();
  delete a;
}

does not print ~B.

it NEEDS virtual ~A(){}