64,646
社区成员
发帖
与我相关
我的任务
分享
class A
{
public:
virtual A* getA()
{
//这里返回A类型的指针
return this;
}
};
class B
{
public:
B* getA()
{
//这里返回B类型的指针
return this;
}
};
//A.h
#ifndef _A_H_
#define _A_H_
using namespace std;
class CWinThread
{
public:
CWinThread(CWinThread *pThread=0 ){ m_pThread=this;};
virtual void Run() { cout <<"CWinThread::Run()" <<endl;}
CWinThread* GetThread(){return this;}
private:
CWinThread* m_pThread;
};
class CWinApp:public CWinThread {
public: CWinApp(){};
void Run() { cout <<"CWinApp::Run()" <<endl;}
};
class MyApp:public CWinApp {
public: MyApp(){};
void Run() { cout <<"Mycpp::Run()" <<endl;}
};
MyApp theApp;
#endif
//A.cpp
#include <iostream>
#include"a.h"
extern MyApp theApp;
int main()
{
CWinApp* pApp=&theApp;
pApp->GetThread()->Run();//结果cout < <"CWinThread::Run()" < <endl;
//说明this并不是指MyApp theApp;
return 0;
}
class A
{
public:
void func () { cout << "A"; }
virtual void vfunc () { cout << "A"; }
};
class B : public A
{
public:
void func () { cout << "B"; }
virtual void vfunc () { cout << "B"; }
};
int main ()
{
B b;
A * pA = &b;
pA->func (); // 输出A
pA->vfunc (); // 输出B
return 0;
}