我想打印(见下文)而不添加任何其他变量或对象......我该怎么做?非常感谢... B :: p r i n A :: druck A :: p r i n t
#include <iostream>
using namespace std;
class A {
public:
void print() { cout << "A: : p r i n t " << endl; }
void druck() { cout << "A: : druck " << endl; }
};
class B : public A {
public:
void print() { cout << "B: : p r i n t " << endl; }
void druck() { cout << "B: : druck " << endl; }
};
int main() {
A * a = new B();
getchar();
return 0;
}
答案 0 :(得分:0)
只需通过B
拨打static_cast
,例如:
a->print();
static_cast<B*>(a)->print();