本文发表在 rolia.net 枫下论坛>>>Dear Prof. Bjarne Stroustrup,
>>>When teaching overloading and overriding of C++, I hope to use the program as
>>follows.
>>>But the Visual C++(6.0 and .net) do not let it be compiled.
>>>>According to inheritance, overloading and overriding concepts,
>>>-----------------------------
>>>b->display(a1) should bind to the b_class::display(a1);
>>>b->play(a1) should bind to the b_class::play(a1); and
>>>b->play(a2) should bind to the b_class::play(a2).
>>>---------------------------------
>>>Do you agree with me?
>
>>No, b_class::display(B) does not override a_class::display(A) because thy
don't
>>have the same argument type.
>>
>>???????????????????????????
>>
>>You are right. b_class::display(B) does not override a_class::display(A)
>because thy don't
>>have the same argument type.
>>
>>But class d_class should inherits b_class::play(A) and b_class::display(A),
>therefore the instance of d_class should understance the message display(A) and
>play(A). Right?
>>
>
>No. the play() and display() functions in the derived class hides the play()
and
>display() functions in the base class. There is no overloading across scopes.
>See: http://www.research.att.com/~bs/bs_faq2.html#overloadderived
>
>>???????????????????????????
>!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>Thank you very much for your reply.
>Yes. I know that the C++ compilers are designed with this idea.
>But we might need to consider overriding. I remember that in C++, overriding is
resolved by a function's signature that includes the function name, the argument
types and the number of the arguments.
>By signature, b_class::play(A) and d_class::play (B), also b_class::display(A)
and d_class::display(B) are different funcitons, they should not be overriden,
right?
>"There is no overloading across scopes. " But the subclass and the base class
should be in the same scope with the idea of inheritance, right?
No. I don't think so. A base class and its derived class are separate scopes.
There are problems with that notion, just as there are problems with the notion
that all members of a class are in a single scope. C++ always supported the
former notion: A base class and its derived class are separate scopes.
>!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>
>>
>>>#include <iostream>
>>>using namespace std;
>>>class A
>>>{
>>> public:
>>> A(){x = 10, y =20;};
>>> int x,y;
>>>};
>>>class B: public A
>>>{
>>>public:
>>> B(){i = 10, j =20;};
>>> int i,j;
>>>};
>>>class b_class
>>>{public: virtual void display(A a)
>>> { cout<<"b_class, a= "<<a.x<<endl;
>>> }
>>> void play(A b)
>>> { cout<<"b_class, b = "<<b.y<<endl;
>>> }
>>>};
>>>class d_class : public b_class
>>>{public: void display(B f )
>>> { cout<<"d_class, f = "<<f.i<<endl;
>>> }
>>> void play(B g )
>>> { cout<<"d_class, g = "<<g.j<<endl;
>>> }
>>>};
>>>void main()
>>>{ b_class *a;
>>> d_class *b, bo;
>>> a = new b_class();
>>> b = new d_class();
>>> A a1,a2;
>>> B b1,b2;
>>> a->display(a1);
>>> a->display(a2);
>>> b->display(a1);//error?
>>> a = b;
>>> a->display(b1);
>>> a->display(b2);
>>> a->play(a1);
>>> a->play(a2);
>>> b->play(a1);//error?
>>> b->play(a2);//error?
>>>}
>>
>> - Bjarne
>>Bjarne Stroustrup, http://www.research.att.com/~bs更多精彩文章及讨论,请光临枫下论坛 rolia.net
>>>When teaching overloading and overriding of C++, I hope to use the program as
>>follows.
>>>But the Visual C++(6.0 and .net) do not let it be compiled.
>>>>According to inheritance, overloading and overriding concepts,
>>>-----------------------------
>>>b->display(a1) should bind to the b_class::display(a1);
>>>b->play(a1) should bind to the b_class::play(a1); and
>>>b->play(a2) should bind to the b_class::play(a2).
>>>---------------------------------
>>>Do you agree with me?
>
>>No, b_class::display(B) does not override a_class::display(A) because thy
don't
>>have the same argument type.
>>
>>???????????????????????????
>>
>>You are right. b_class::display(B) does not override a_class::display(A)
>because thy don't
>>have the same argument type.
>>
>>But class d_class should inherits b_class::play(A) and b_class::display(A),
>therefore the instance of d_class should understance the message display(A) and
>play(A). Right?
>>
>
>No. the play() and display() functions in the derived class hides the play()
and
>display() functions in the base class. There is no overloading across scopes.
>See: http://www.research.att.com/~bs/bs_faq2.html#overloadderived
>
>>???????????????????????????
>!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>Thank you very much for your reply.
>Yes. I know that the C++ compilers are designed with this idea.
>But we might need to consider overriding. I remember that in C++, overriding is
resolved by a function's signature that includes the function name, the argument
types and the number of the arguments.
>By signature, b_class::play(A) and d_class::play (B), also b_class::display(A)
and d_class::display(B) are different funcitons, they should not be overriden,
right?
>"There is no overloading across scopes. " But the subclass and the base class
should be in the same scope with the idea of inheritance, right?
No. I don't think so. A base class and its derived class are separate scopes.
There are problems with that notion, just as there are problems with the notion
that all members of a class are in a single scope. C++ always supported the
former notion: A base class and its derived class are separate scopes.
>!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>
>>
>>>#include <iostream>
>>>using namespace std;
>>>class A
>>>{
>>> public:
>>> A(){x = 10, y =20;};
>>> int x,y;
>>>};
>>>class B: public A
>>>{
>>>public:
>>> B(){i = 10, j =20;};
>>> int i,j;
>>>};
>>>class b_class
>>>{public: virtual void display(A a)
>>> { cout<<"b_class, a= "<<a.x<<endl;
>>> }
>>> void play(A b)
>>> { cout<<"b_class, b = "<<b.y<<endl;
>>> }
>>>};
>>>class d_class : public b_class
>>>{public: void display(B f )
>>> { cout<<"d_class, f = "<<f.i<<endl;
>>> }
>>> void play(B g )
>>> { cout<<"d_class, g = "<<g.j<<endl;
>>> }
>>>};
>>>void main()
>>>{ b_class *a;
>>> d_class *b, bo;
>>> a = new b_class();
>>> b = new d_class();
>>> A a1,a2;
>>> B b1,b2;
>>> a->display(a1);
>>> a->display(a2);
>>> b->display(a1);//error?
>>> a = b;
>>> a->display(b1);
>>> a->display(b2);
>>> a->play(a1);
>>> a->play(a2);
>>> b->play(a1);//error?
>>> b->play(a2);//error?
>>>}
>>
>> - Bjarne
>>Bjarne Stroustrup, http://www.research.att.com/~bs更多精彩文章及讨论,请光临枫下论坛 rolia.net