本文发表在 rolia.net 枫下论坛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
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