High quality C++/C Programming Guide-8th Chapter-c++ function Advanced Features (2)

Source: Internet
Author: User
Tags include

8.1.3 Beware of implicit type conversions resulting in overloaded functions ambiguity

In the example 8-1-3, the argument for the first output function is type int, and the second output function is the float type. Because the number itself has no type, a number is automatically typed as an argument (called an implicit type conversion). Statement output (0.5) produces a compilation error because the compiler does not know whether to convert 0.5 to int or to a float type parameter. Implicit type conversions can simplify the writing of programs in many places, but can also leave pitfalls.

# include <iostream.h>

void output (int x); function declaration

void output (float x); function declaration

void output (int x)

{

cout << "Output int" << x << Endl;

}

void output (float x)

{

cout << "Output float" << x << Endl;

}

void Main (void)

{

int x = 1;

Float y = 1.0;

Output (x); Output int 1

Output (y); Output Float 1

Output (1); Output int 1

Output (0.5); error! Ambiguous call because of automatic type conversion

Output (int (0.5)); Output int 0

Output (float (0.5)); Output float 0.5

}

Example 8-1-3 an implicit type conversion causes overloaded functions to produce ambiguity

Overload, overlay, and hide of 8.2 member functions
The overloading, overwriting (override) of member functions is easily confused with concealment, and C + + programmers must be clear about concepts, or errors will be hard to find.

8.2.1 Overload and Overlay

Features that are overloaded by member functions:

(1) The same range (in the same class);

(2) The function name is the same;

(3) different parameters;

(4) virtual keyword is optional.

Overlay refers to a derived class function that overrides a base class function, characterized by:

(1) different ranges (in derived classes and base classes, respectively);

(2) The function name is the same;

(3) the same parameter;

(4) The base class function must have the virtual keyword.

In the example 8-2-1, the function base::f (int) and base::f (float) are overloaded with each other, and base::g (void) is overwritten by derived::g (void).

#include <iostream.h>

Class Base

{

Public

void f (int x) {cout << "base::f (int)" << x << Endl;}

void f (float x) {cout << "base::f (float)" << x << Endl;}

virtual void g (void) {cout << "base::g (void)" << Endl}

};

Class Derived:public Base

{

Public

virtual void g (void) {cout << "derived::g (void)" << Endl}

};

void Main (void)

{

Derived D;

Base *PB = &d;

Pb->f (42); Base::f (int) 42

Pb->f (3.14f); Base::f (float) 3.14

Pb->g (); Derived::g (void)

}

Example 8-2-1 a member function overload and overwrite

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.