由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++里,Base Class如何调用Derived Class的method
相关主题
simple question on C++ initialization listfunc调用结束时出错
抠字眼:assignment and initialize in C++求助 怎么编辑 多个 .c files(比如a.c, b.c) 和一个.h file(ab (转载)
C/C++函数调用和栈内存static initialization dependency c++
问一个 copy constructor 的问题 (C++)一道 memset in C++的题
C++ set ctor的疑问c++ 不自动initialize变量么?
find bugs of c++ codesPython 如何自动import multiple files
问一个C++的问题为啥 const Base cb 要求Base() {} 而 const vBase vb 不呢?
这个函数有问题吗?全局对象
相关话题的讨论汇总
话题: class话题: derived话题: base话题: 调用话题: public
进入Programming版参与讨论
1 (共1页)
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
3
不可以
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
7
I see. thanks!
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
10
不可以
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
14
I see. thanks!
1 (共1页)
进入Programming版参与讨论
相关主题
全局对象C++ set ctor的疑问
这个是什么原因find bugs of c++ codes
请问c++为什么会编译失败?问一个C++的问题
谁来解释一下这个是compiler问题吗?这个函数有问题吗?
simple question on C++ initialization listfunc调用结束时出错
抠字眼:assignment and initialize in C++求助 怎么编辑 多个 .c files(比如a.c, b.c) 和一个.h file(ab (转载)
C/C++函数调用和栈内存static initialization dependency c++
问一个 copy constructor 的问题 (C++)一道 memset in C++的题
相关话题的讨论汇总
话题: class话题: derived话题: base话题: 调用话题: public