#include <stdio.h>
#include <vector>
using namespace std;
class Base
{
public :
Base(){} ;
virtual ~Base() {} ;
public :
virtual void Draw() { printf("this is base Draw!") ; };
};
class Deprived : public Base
{
public :
Deprived() {} ;
virtual ~Deprived() {} ;
public :
void Draw() { printf("this is Deprived Draw!") ; };
#include <stdio.h>
#include <vector>
using namespace std;
class Base
{
public :
Base() { printf("this is base constructor!\n") ; } ;
virtual ~Base() { printf("this is base deconstructor!\n") ;} ;
Base(const Base& b)
{
printf("this is base copy constructor!\n") ;
}
const Base& operator=(const Base& b)
{
printf("this is base equal fun!\n") ;
return *this ;
}
public :
virtual void Draw()= 0 { printf("this is base Draw!\n") ; };
};
class Deprived : public Base
{
public :
Deprived() {printf("this is deprived constructor!\n") ;} ;
Deprived(const Deprived& d) {printf("this is deprived copy constructor!\n") ;} ;
virtual ~Deprived() {printf("this is deprived deconstructor!\n") ;} ;
const Deprived& operator=(const Deprived& b) {
printf("this is deprived equal fun!\n") ;
return *this ;
} ;
public :
void Draw() { printf("this is Deprived Draw!\n") ; };
};
int main(int argc, char* argv[])
{
{
vector< Base *> objs ;
Deprived *dep = new Deprived() ;
objs.push_back( dep ) ;
for( vector<Base *>::iterator it = objs.begin() ; it!= objs.end() ; it++ )
{
(*it)->Draw() ;
delete (*it) ;
}
objs.clear() ;
}
return 0;
}
输出为:
this is base constructor!
this is deprived constructor!
this is Deprived Draw!
this is deprived deconstructor!
this is base deconstructor!
Press any key to continue
虽然这样可以,但终觉不爽,哪位大侠给予指点,不甚感激!