C++ 如何为下面的程序使用静态或动态强制转换
请您帮助我如何使用Static或Dynamci cast调用派生类非虚函数 我只想使用静态或动态强制转换来调用派生调用函数“test”C++ 如何为下面的程序使用静态或动态强制转换,c++,C++,请您帮助我如何使用Static或Dynamci cast调用派生类非虚函数 我只想使用静态或动态强制转换来调用派生调用函数“test” #include<iostream> using namespace std; class base { public: virtual void call() { cout<<"I am base"<<endl; } /*voi
#include<iostream>
using namespace std;
class base
{
public:
virtual void call()
{
cout<<"I am base"<<endl;
}
/*void test()
{
cout<<"I am test"<<endl;
}*/
};
class derive:public base
{
public:
void call()
{
cout<<"I am derive"<<endl;
}
void test()
{
cout<<"I am derived test"<<endl;
}
};
int main()
{
derive d;
base *bptr = &d;
bptr->call();
derive* dptr = dynamic_cast<derive *>(base);
dptr->test();
return 0;
}
#包括
使用名称空间std;
阶级基础
{
公众:
虚拟无效调用()
{
cout记住dynamic\u cast(ptr)
改变
derive* dptr = dynamic_cast<derive*>(base);
因为base
是一个类型(如果您在所有类型/类的第一个字母上使用大写字母,您会注意到您使用的是一个类型而不是一个变量)。记住dynamic\u cast(ptr)
改变
derive* dptr = dynamic_cast<derive*>(base);
因为base
是一种类型(如果您在所有类型/类的第一个字母上使用大写字母,您会注意到您使用的是一种类型而不是一个变量)。您的意思是对bptr进行大小写,而不是对base(类型)进行大小写吗?
derive* dptr = dynamic_cast<derive*>(bptr);
I am derive
I am derived test