Differences between heavy load, rewriting, redefinition, and redirection in C ++

Source: Internet
Author: User

First, reload:

Overload refers to the overload of functions, and there are reloads of member functions and general functions. There is no difference between the two, but it is only in different places.

Function overloading indicates that the function name is the same, but the parameters are different. Function overloading does not exist in the C language, but is limited to C ++ as follows:

Double max (Double X, Double Y ),

Int max (int x, int y );

These two functions are examples of overloading. How does the compiler determine which function to call? In booksProgramIt will identify which one to call based on the parameter type. In fact, it is a dumb.

In fact, during compilation, the compiler has changed the function name and added some suffixes (of course, automatic identification is not enough)

For example, the above two programs are compiled into max_double_double and max_int_int respectively. Of course, Here I just want to show it. Actually, this is not the case. If you want to see the name, you can locate the program through the command line. There is a tutorial on Baidu, i'm not tired of talking about it!

Rewrite again:

Function rewriting occurs in the base class and the derived class. The base class and the derived class function must have the same name and the base class function must have a virtual modifier. The result is that the derived class function overwrites the base class function, in C ++ object-oriented programming

I am very familiar with the polymorphism, as shown below:

Base * P = new child;

P-> display ();

If the display function is rewritten for a derived class, the derived class is called here. Otherwise, the base class is called!

Then redefine

There are two cases, that is, between the base class and the derived class.

First, the base class function does not include the vistual modifier. In the derived class, there is a function with the same name as the base class function and the same parameter. In this case, the base class function is blocked in the derived class, this is called a redefinition!

Type 2: if the function of the derived class and the function of the base class have the same name but different parameters, the function of the base class is hidden no matter whether it is virtual or not.

I will explain the second case. Some people may say that I want to write a function with the same name as the base class function but different parameters in the derived class, it is overloaded with the inherited base-class functions!

I believe many new users will have this idea (I used it in the past). c ++ primer mentioned this question. If you want to reload the functions inherited from the base class, you must manually copy the function declaration from the base class to the derived class. For what reason, I think it is related to the bottom layer of the compiler, so I will not go into it!

Final redirection

In fact, this redirection has nothing to do with the function, but it is similar to the previous concepts. He was mentioned during Io stream processing.

In the C language, we know that a file pointer is associated with a file, and the redirection only requires a freopen function to locate a new file. This is called the C language redirection.

In C ++, there are two overload functions for stream redirection:
Streambuf * rdbuf () const;
Streambuf * rdbuf (streambuf *)

As follows:

Streambuf * Backup;
Ifstream fin;
Fin. Open ("data. In ");
Backup = cin. rdbuf (); // back up CIN's streambuf
Cin. rdbuf (Fin. rdbuf (); // assign file's streambuf to CIN
//... CIN will read from File
Cin. rdbuf (Backup); // restore CIN's original streambuf

Note that we finally used cin. rdbuf (Backup) to redirect CIN back to the console!

It's almost 6 o'clock. after a night's study, it's time to go to bed. I'm a little bit confused, and I'm dragging my tired body to write it out. Some concepts are not well written. Please be considerate!

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.