继承中派生类指针转换为基类指针

#include "stdafx.h"
#include 
using namespace std;

class Base
{
public:
	Base(){}
	~Base(){}

	virtual void Deal()
	{
		int n;
		n = 6;
	}
private:
};

class A : public Base
{
public:
	A(){}
	~A(){}

	virtual void Deal() override
	{
		int i;
		i = 1;
	}

	void xxxx()
	{

	}
private:
};

int _tmain(int argc, _TCHAR* argv[])
{
	shared_ptr a(new A());
	shared_ptr p = a;
	p->Deal();//ok,继承覆盖了基类Deal函数

	p->xxxx();//class "Base" 没有成员 "xxxx"

	return 0;
}

如何才能调用继承类(A类)的成员函数xxxx呢?

转换方法:

      shared_ptr  sp = dynamic_point_cast(p);

此时,即可执行A类中的函数  sp.xxxx(); 了.

智能指针用:dynamic_pointer_cast, 

 一般指针用:dynamic_cast




你可能感兴趣的:(继承中派生类指针转换为基类指针)