C++ 致电家长';s类外的公共成员函数
下面是我想在函数C++ 致电家长';s类外的公共成员函数,c++,C++,下面是我想在函数main中执行的一个示例。 例如,观察我在两行之间的评论: #include <stdio.h> class A { public: void msg() { puts("from A"); } }; class B : public A { public: void msg() { puts("from B"); } }; int main() { A a; B b
main
中执行的一个示例。
例如,观察我在两行之间的评论:
#include <stdio.h>
class A {
public:
void msg()
{
puts("from A");
}
};
class B : public A {
public:
void msg()
{
puts("from B");
}
};
int main()
{
A a;
B b;
a.msg();
b.msg(); // This must print out B
b.msg(); // And I want this to print A. What is the syntax for that?
}
#包括
甲级{
公众:
void msg()
{
出售(“从A”);
}
};
B类:公共A{
公众:
void msg()
{
看跌期权(“来自B”);
}
};
int main()
{
A A;
B B;
a、 msg();
b、 msg();//这必须打印出b
b、 msg();//我想把它打印成A。它的语法是什么?
}
我不想添加额外的代码到这个,只是可能是一些合成糖。类似于
A::b.msg
,但它不起作用您可以使用以下命令:
b.A::msg(); //will call msg from the class A
但也许你应该换一种模式?你可以使用以下方法:
b.A::msg(); //will call msg from the class A
但也许你应该看看另一种模式?使用b.a::msg()代码>您希望通过这样做解决的实际问题和原始问题是什么?使用b.A::msg()代码>您希望通过这样做解决的实际问题和原始问题是什么?