C ++ deficiency series (4)

Source: Internet
Author: User

Function overload
C ++ allows you to overload functions with different parameter types. Overloaded functions and functions with polymorphism (that is, virtual functions)
) The difference is that calling the correct entity of the overloaded function is determined during compilation.
The state function calls the function entity we want to call through dynamic binding during running. Polymorphism
It is achieved through redefinition (or rewriting. Do not overloading or overr
IDing. The overload occurs when two or more functions have the same name. Differentiate it
The method is to check the number or type of their parameters. Multi-distribution (Mul) in heavy load and clos
Different tiple dispatching), multiple distribution of parameters is completed by polymorphism during runtime.
[Reade 89] indicates the differences between overload and polymorphism. Overloading means using the same in the same context
In place of different function entities (they have completely different definitions and parameter types ). Only
Has a definition body, And all types are child types derived from one of the most basic types. C.
Strachey pointed out that polymorphism is a parameterized polymorphism, while Overloading is a special polymorphism. Used to judge differences
Function signature ).
Overload is useful in the following example:
Max (INT, INT)
Max (real, real)
This ensures that the best Max function entity relative to the int and real types is called. However, Object-Oriented Programming
This function provides a variable. The object itself is passed to the function as a hidden parameter (in C ++
, We call it this ). In this way, the object-oriented concept implicitly contains a type
But there are more restrictions. A simple example of the above discussion is as follows:
Int I, J;
Real r, s;
I. Max (j );
R. Max (s );
But if we write: I. Max (R), or R. Max (j), the compiler will tell us that there is a class in it.
Type Mismatch Error. Of course, through the operation of the overload operator, such behavior can be better expressed as follows
:
I Max J or
R max S
However, Min and Max are special functions. They can accept two or more parameters of the same type, and
It can also be used on arrays of any length. Therefore, the most common code form in Eiffel
It looks like this:
Il: comparable_list [integer]
RL: comparable_list [real]
I: = Il. Max
R: = RL. Max
The above example shows that the object-oriented programming model (paradigm), especially the combination
In combination, function overloading can also be achieved without the declaration form like function overloading in C ++. However
C ++ makes this concept more general. The advantage of C ++ is that we can use more than one parameter
Instead of using a hidden current object as a parameter.
Another factor we need to consider is to determine which overload function is called after the compilation stage.
But for rewriting, It is pushed back to the runtime. In this way, it seems that the overload can make us get
More performance benefits. However, in the process of global analysis, the compiler can check whether the min and Max functions are in
And then you can directly call them (If yes ). This means that the compiler checks
Object I and R, and then analyze the max functions corresponding to them, and find that in this case no polymorphism is included
So the above statement produces the target code that calls Max directly. In contrast, if object n
Is defined as a number, and number provides an abstract Max function declaration (the real. Max and int
Erger. Max is inherited from it), then the compiler will generate dynamically bound code. This is because n
It may be integer or real.
Do you think this method of C ++ is very useful (that is, function Overloading is implemented by providing different parameters? No
You must also understand that object-oriented programming has various limitations and there are many rules. C ++ is
It is implemented by specifying that the parameter must be consistent with the base class. The parameter in the input function can only be a base class or
The derived class of the base class. For example:
A. F (B someb ){...}
Class B ...;
Class D: Public B ...;
A;
D;
A. F (d );
Where D must be consistent with the class 'B', which the compiler will detect.
Another feasible method for implementing function overloading through different function signatures is to assign different functions
Use different names to make their signatures different. We should use names to distinguish different entities (
Tities. The compiler can cross-check whether the provided real parameters meet the shape parameters required by the specified function.
. This also results in a better self-document for the software ). Select a pointer from a similar name
Fixed entities are usually not easy, but their benefits are indeed worth doing.
[Wiener95] provides an example to demonstrate possible problems with overloaded virtual functions:
Class Parent
{
Public:
Virutal int doit (int v)
{
Return v * V;
}
};
Class child: Public parent
{
Public:
Int doit (int v, int AV = 20)
{
Return v * AV;
}
};
Int main ()
{
Int I;
Parent * P = new child ();
I = p->; doit (3 );
Return 0;
}
After the program is executed, what will I be equal? Some may think it is 60, but the result is 9. This is because in chil
The doit signature in D is inconsistent with that in parent. It does not overwrite the doit in parent, but simply reloads
In this case, the default value does not work.
Java also provides method overloading. Different methods can have the same name and different signatures.
New technologies are not introduced in Eiffel. Instead, they are modeled, inherited, and redefined. Eiffel provides covariant
This means that the function in the subclass does not need to fully comply with the signature in the parent class,
L's strong type detection technology can make them match each other.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.