Name occlusion problem in C ++ inheritance system

xiaoxiao2021-03-06  88

The problem with the same name function in the hide base class in C is more puzzled, and it is necessary to explain it. Take a look at the following code:

Class base {public: Virtual void f (int x) {};}; class derived: public base {public: Virtual void f (void * p) {};}; derived * pd = new derived; pd-> f 10); // Compiling errors

For such a case, Scott Meyers explained this (Effective C Chinese Second Edition No. 50), Derived :: f blocked Base :: F, even if the parameter type of both is different. That is to say, the compiler does not know that there is a Base :: F existence of the type of parameter INT. But the following code can be compiled:

Base * pb = new deerived; pb-> f (10);

The interpretation of Bjarne Stroustrup in ARM is: Suppose When you call F, you really want to call the time Derive version, but you accidentally use the wrong parameter type (this example is int). Further hypothesis is a member of the inheritance system, and you don't know that Derive has inherited a baseclass, the latter declares that there is a virtual function F and require an int parameter. In this case, you will accidentally call Baseclass :: F - one you don't even know the function it exists. Such an error may often occur in a large inheritance system, so Stroustrup determines the bottom salary (good at the bottom of the salary!) Base Class Members with Derived Class Members. Take a look at the following code:

Class base {public: Virtual void f (int x) {}; virtual void f (char * x) {};

Class Derived: Public Base {public: Virtual Void f (int x) {};

Derived * pd = new derived; pd-> f ((char *) 2); // Compiling error PD-> f (2); // passed Base * Pb = new deerid; pb-> f ((char *) 2); // through PB-> f (2); // pass

According to the above explanation, it should not be difficult to understand such compilation results. Then let's see if the static function is the same.

Class base {public: static void f (int x) {};}; class deact: public base {public: static void f (void * x) {}; // No to STATIC before this definition, there is the following results }; Derived * pd = new derived; pd-> f (2); // Compiling error PD-> f (void *) 2); // passed Base * Pb = new deerid; pb-> f (2) ; // via PB-> F ((void *) 2); // Error This we find the shadow function as described above for the Static function. C allows derived classes to redefine any of the functions in the base class (except for Private), this power is a double-edged sword, which also induces them to make mistakes while giving programmers. Class base {public: static void f (int x) {};}; class deact: public base {public: virtual void f (int x) {};}; class Son: public derived {public: Virtual Void f (int x) {};}; son * ps = new Son; derived * pd = ps; base * pb = pd; ps-> f (2); // call SON:: F PD-> f (2); / / Call SON:: F PB-> f (2); // Call Base: F or above This kind of schizophrenia when developing inheritance hierarchy is difficult to find the root source.

转载请注明原文地址:https://www.9cbs.com/read-101126.html

New Post(0)