static_cast配合void*实现的多态


#include "stdafx.h"
#include 
#include 
#include 

using namespace std;

class X
{
public:
    virtual void f(){ cout << "x"; }
};

class B: public X
{
public:
    //virtual void f(){ cout << "b"; }
};

class A : public X
{
public:
    virtual void f(){ cout << "a"; }
};

class D : public A
{
public:
    virtual void f(){ cout << "d"; }
};

class E : public A
{
public:
    virtual void f(){ cout << "e"; }
};

class M
{
public:
    virtual void f(){ cout << "m"; }
};

class N
{
public:
    virtual void f(){ cout << "n"; }
};

int _tmain(int argc, _TCHAR* argv[])
{
    //int max[1000][1000];
    /*vector vec;
    vec.push_back(new D);
    vec.push_back(new A);
    vec.push_back(new B);
    vec.push_back(new E);
    vec.push_back(new X);

    for (vector::iterator it = vec.begin(); it != vec.end(); ++it)
    {
        static_cast(*it)->f();
    }

    static_cast((void*)new D)->f();*/

    static_cast((void*)new N)->f();



    return 0;
}

你可能感兴趣的:(C++)