z*****n 发帖数: 447 | 1 比如:
#include
class f{
public:
virtual void p(){printf("father\n");}
};
class c : public f{
public:
void p(){
printf("child\n");
}
};
--------------------------------------
Derived Class 调用Base Class可以这样:
f *a = new c;
a->f::p(); // 这里Print的是"father"
---------------------------------------
如果
f *b = new f;
b怎样才可以调用到 c的method p()
多谢! |
s*********t 发帖数: 1663 | 2 不要这么做
【在 z*****n 的大作中提到】 : 比如: : #include : class f{ : public: : virtual void p(){printf("father\n");} : }; : class c : public f{ : public: : void p(){ : printf("child\n");
|
f*******n 发帖数: 12623 | |
y**b 发帖数: 10166 | 4 1.设计虚函数的目的是什么?多态。如果不用于多态,设计上就可能有欠考虑。
2.基类对象、指针、引用只能访问对象的基类部分,不能访问访问基类中没有
定义的部分。虚函数也不该例外。
本着对C++的兴趣我争取能回答一些基础问题,如果有错大牛们请及时指正。
【在 z*****n 的大作中提到】 : 比如: : #include : class f{ : public: : virtual void p(){printf("father\n");} : }; : class c : public f{ : public: : void p(){ : printf("child\n");
|
O*******d 发帖数: 20343 | 5 Base class不应该知道derived class的任何东西。
【在 z*****n 的大作中提到】 : 比如: : #include : class f{ : public: : virtual void p(){printf("father\n");} : }; : class c : public f{ : public: : void p(){ : printf("child\n");
|
c**********e 发帖数: 2007 | 6
> f *b = new f;
You can not use base class pointer to initialize derived class pointer.
【在 z*****n 的大作中提到】 : 比如: : #include : class f{ : public: : virtual void p(){printf("father\n");} : }; : class c : public f{ : public: : void p(){ : printf("child\n");
|
z*****n 发帖数: 447 | |
z*****n 发帖数: 447 | 8 比如:
#include
class f{
public:
virtual void p(){printf("father\n");}
};
class c : public f{
public:
void p(){
printf("child\n");
}
};
--------------------------------------
Derived Class 调用Base Class可以这样:
f *a = new c;
a->f::p(); // 这里Print的是"father"
---------------------------------------
如果
f *b = new f;
b怎样才可以调用到 c的method p()
多谢! |
s*********t 发帖数: 1663 | 9 不要这么做
【在 z*****n 的大作中提到】 : 比如: : #include : class f{ : public: : virtual void p(){printf("father\n");} : }; : class c : public f{ : public: : void p(){ : printf("child\n");
|
f*******n 发帖数: 12623 | |
y**b 发帖数: 10166 | 11 1.设计虚函数的目的是什么?多态。如果不用于多态,设计上就可能有欠考虑。
2.基类对象、指针、引用只能访问对象的基类部分,不能访问访问基类中没有
定义的部分。虚函数也不该例外。
本着对C++的兴趣我争取能回答一些基础问题,如果有错大牛们请及时指正。
【在 z*****n 的大作中提到】 : 比如: : #include : class f{ : public: : virtual void p(){printf("father\n");} : }; : class c : public f{ : public: : void p(){ : printf("child\n");
|
O*******d 发帖数: 20343 | 12 Base class不应该知道derived class的任何东西。
【在 z*****n 的大作中提到】 : 比如: : #include : class f{ : public: : virtual void p(){printf("father\n");} : }; : class c : public f{ : public: : void p(){ : printf("child\n");
|
c**********e 发帖数: 2007 | 13
> f *b = new f;
You can not use base class pointer to initialize derived class pointer.
【在 z*****n 的大作中提到】 : 比如: : #include : class f{ : public: : virtual void p(){printf("father\n");} : }; : class c : public f{ : public: : void p(){ : printf("child\n");
|
z*****n 发帖数: 447 | |