Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the overloading, inheritance, overriding and hiding of C++ member functions

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the overloading, inheritance, overriding and hiding of C++ member functions". In daily operation, I believe many people have doubts about what is the overloading, inheritance, overwriting and hiding of C++ member functions. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation, hoping to help you answer the doubts about "what is the overloading, inheritance, coverage and hiding of C++ member functions". Next, please follow the editor to study!

I. overloading of C++ member functions

There are four kinds of member functions in C++, which are ordinary member functions, virtual virtual functions and const member functions.

(1) void func (int a)

(2) virtual void func (int a)

(3) void func (int a) const

If you declare these four functions in a class, what is the duplicate definition? Which are overloads?

Where (1) (2) is a duplicate definition, so the compilation cannot pass, while (3) and (1) (2) are different types of functions and are overloaded.

The characteristics of member functions being overloaded are:

(1) have the same scope (that is, in the same class definition)

(2) the function name is the same

(3) different types, order or number of parameters (including const parameters and non-const functions)

(4) virtual keyword is optional.

From the overloaded feature of the member function, we can know that (1) (2) is a duplicate definition. So why is (3) different from (1) (2)?

Because functions in the class automatically add their own class pointer this, so

Void func (int a) = void func (Base * this, int a) virtual func (int a) = virtual func (Base * this, int a) void func (int a) const = = void func (const Base * this, int a) const

So (3) can be overloaded with (1) (2) because the parameter has a const.

II. Inheritance of C++ member functions

# include using namespace std; class Base {public: void f (int a) {cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report