[Notes] Use the same name function of different base classes under more inherited

xiaoxiao2021-03-06  43

When you inherit the two-oriented base class with the same name, the derived class object references the name function, and if you do not do any processing, it will not work properly. The reason is that the overloading rules do not apply to the situation across multiple classes. There are two basic methods: First, it is clearly referred to when using which base class is used; the other is to introduce the same name function of different base classes in a public scope by using the USING statement.

The first method is exemplified.

Class a {

PUBLIC:

INT func (int);

Void func (char);

// ...

}

Class b {

PUBLIC:

Double func (double);

// ...

}

Class AB: Public A, Public B {PUBLIC B {

PUBLIC:

// ...

}

Void F1 (AB * AB)

{

AB-> Func (1); // Ambiguity

AB-> A :: func (1); / / Clearly point out which base class with the same name

AB-> B :: Func (1);

}

Second method example:

In the derived class, through the USING statement, introduce the same name in the base class into a public domain so that the overloading of the resolution rules can be used normally.

Class AB: Public A, Public B {PUBLIC B {

PUBLIC:

Using a :: f; // pay attention to grammar

Using b :: f;

Char func (char); // covered A :: Func (char)

Ab func (ab);

// ...

}

Void F2 (AB * AB)

{

AB-> Func (1); // a :: func (int)

AB-> Func (0.5); // b :: func (double)

AB-> FUNC ('c'); // Ab :: func (char)

AB-> FUNC (* ab); // Ab :: func (ab)

}

In particular, there is no change in the coverage mechanism of the virtual function.

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

New Post(0)