C + + differs from C because the function overload

Source: Internet
Author: User

In the C language, a pointer can point to a function. This pointer also has two properties, but one is the entry address of the function, and the other is the return type of the function. For example, the following program, which is correct in the C language:

int time12 (int i)

{return (I%12);

}

int main ()

{int (*FP) () =time12;

INTT=FP (13);

Return0;

}

The first sentence of the main function is a definition statement. We should read from the identifier to the left of the equals sign, except for identifiers that appear on the left side of the equals sign, which are read in the order in which the symbol is used as the operator. This punctuation: FP is a pointer, it points to a function (note that the C language allows its parameter types not to be written out), the return value of this function is int, and this pointer is initialized to the entry address of the function time12.

However, the above procedure in C + +, the first statement is considered to be wrong. C + + is a strongly typed check language, which is related to the function overload mechanism of C + +. C + + requirements must indicate the type of all formal parameters of the function. The following program is the correct C + + program:

int time12 (int i)

{return (I%12);

}

int main ()

{int (*FP) (int) =time12;

INTT=FP (13);

Return0;

}

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.