Here you are! THANKS.

bills (Bill)
本文发表在 rolia.net/zh 相约加拿大网上社区枫下论坛
Here is an example, is there anybody can tell me why the line 28 can not match , but the line 30 works.
AS my understanding, the only difference is
>> the line28 will use polymorphism and line 30 not.

and in general, this question can be described as:
if base class has several virtual member functions
with same name but different parameters, and then the
derived calss only override one of these functions,
can we create the derived OBJECT from derived class (
derivedclass * dc = new derivedclass), and call those
function whicj are not be overridden by the derived class),

or the only way is use the polymorphism to create the derived class ( baseclass *dc2= new derivedclass) ?


What I want to know is the reason? Why it doesnot work?
not how can we make it works, 'cause I know how. ;))


1 #include <stdio.h>
2 #include
3
4 class BASE
5 {
6 public:
7 BASE() {};
8 ~BASE() {};
9 virtual fun() { cout << " BASE fun()" << endl; } ;
10 virtual fun(int ) { cout << " BASE fun(int)" << endl; } ;
11 virtual fun(int , int) { cout << " BASE fun(int )" << endl; } ;
12 };
13
14 class DETRIVED: public BASE
15 {
16 public:
17 DETRIVED() {};
18 ~DETRIVED() {};
19 fun() { cout << " DETRIVED fun()" << endl; } ;
20 };
21
22
23 int main()
24 {
25 DETRIVED *d1= new DETRIVED();
26 BASE *d2= new DETRIVED();
27
28 d1->fun(1);
29
30 d2->fun(1);
31 return 0;
32 }





"test.cc" 32 lines, 541 characters
[ public]> g++ test.cc
g++ test.cc
test.cc: In function `int main()':
test.cc:28: no matching function for call to `DETRIVED::fun (int)'
test.cc:19: candidates are: DETRIVED::fun()

[public]>
更多精彩文章及讨论,请光临枫下论坛. 网址: rolia.net/zh
(#133846@0)
2001-7-16 -05:00

回到话题: A question about C++ polymorphism & inheritence

回到论坛: HOME枫下论坛枫下论坛主坛工作学习English

URL:   
http://www.rolia.net/zh/post.php?f=0&p=133846