C ++ Primer learning note _ 28 _ Operator Overloading and conversion (3) -- reload, overwrite and hide member functions, type conversion operators, * operator overloading,-& gt; operator overload, operator overload type conversion

Source: Internet
Author: User

C ++ Primer learning note _ 28 _ Operator Overloading and conversion (3) -- reload, overwrite and hide member functions, type conversion operators, * operator overloading, and-> Operator Overloading, operator overload type conversion
C ++ Primer learning note _ 28 _ Operator Overloading and conversion (3) -- reload, overwrite and hide member functions, type conversion operators, * operator overloading, and-> Operator Overloading
I. Reload, overwrite, and hide of member functions with the same name in the class hierarchy. There are three relationships: Reload, overwrite, and hide, which help to write high-quality code.

1. overload of member functions
The concept of overload is relatively simple. Only a member function with the same name in the same definition has an overload relationship. the type and number of parameters of the function vary depending on the main features; however, the number and type of function parameters cannot be the same, but the function is differentiated by different return value types, which is exactly the same as that of normal functions. In addition, the overload has nothing to do with whether the member functions are virtual functions. For example:

class A{    ...    virtual int fun();    void fun(int);    void fun(double, double);    static int fun(char);    ...};
The four fun functions in the above class A definition are overload relationships.
Features of member functions being overloaded:
(1) the same range (in the same class );
(2) Same function name;
(3) different parameter lists;
(4) virtual keywords are optional.

2. Coverage of member functions
Overwrite refers to overwriting functions of the same name in the base class in the derived class. The base class function must be a virtual function and:
(1) There are the same number of parameters as the virtual functions of the base class;
(2) virtual functions of the base class have the same parameter type;
(3) It has the same return type as the virtual function of the base class: it is the same as the virtual function of the base class, or both return pointers (or references ), in addition, the pointer (or reference) type returned by the virtual function of the derived class is the child type (derived type) of the pointer (or reference) type returned by the replaced virtual function in the base class ).
In the following code, fun in B overwrites fun in.
class A{public:    virtual void fun(int) { }};class B: public A{public:    void fun(int) { }};
The covered features are as follows:
(1) different scopes (located in the derived class and the base class respectively)
(2) Same function name;
(3) Same Parameters
(4) basic functions must have virtual keywords.

The differences between heavy load and coverage are as follows:
(1) coverage is the relationship between the Child class and the parent class, and is a vertical relationship. During heavy load, the relationship between different methods in the same class is a horizontal relationship;
(2) The overwrite request parameter list is the same, and the reload request parameter list is different; the overwrite request returns the same type, and the overload request is different;
(3) In the override relationship, the called method body is determined based on the object type, and the overload relationship is selected based on the real parameter table and the form parameter table during the call.

3. Hide member functions
Hiding means that in some cases, functions in the derived class shield functions of the same name in the base class, including:
(1) The two function parameters are the same, but the base function is not a virtual function. The difference from overwriting is whether the base-class function is a virtual function. As follows:
Class A {public: void fun (int) {}}; class B: public A {public: void fun (int) {}// hide the fun function of the parent class };
If the above Code is:
B b;b.fun(2);
The function fun in B is called. If you need to call the function fun in A, you can call it in the following form:
B b;b.A::fun(2);


(2) The two function parameters are different. The base function is blocked regardless of whether the base function is a virtual function or not. The difference between the two functions and the overload function is that the two functions are not in the same class. See the following code:
Class A {public: virtual void fun (int) {}}; class B: public A {public: void fun (char) {}// hide the fun function of the parent class };
If the above Code is:
B b;b.fun(2);
It is wrong because the function fun in A is hidden. At this time, the compiler cannot find the fun function with the int parameter. In this case, you can use:
B b;b.A::fun(2);
Call the fun function in.

[Example 1]
Analyze the running results of the following programs ()
class A{public:    A() {cout << "construct A" << endl;}    ~A() {cout << "destroy A" << endl;}};class B: public A{public:    B() {cout << "construct B" << endl;}    ~B() {cout << "destroy B" << endl;}};void main(){    B obj;}
A. construct
Construct B
Destroy A destroy B

B. construct
Construct B
Destroy B destroy
Answer: B. The constructor of the base class is executed first, and the structure sequence is the opposite to the execution sequence of the constructor.

[Example 2]
In the following code, the constructor and destructor of A are executed () times?
A *pa = new A[10];delete []pa;
A, 1, 1 B, 10, 10 C, 1, 10 D, 10, 1
Answer: B. Array pa has A total of 10 Class A objects, and each object needs to execute constructor and destructor.


Ii. type conversion Operators

Must be a member function, not a friend Function
No Parameters
The return type cannot be specified.
Function prototype: operator type name ();


#include <iostream>using namespace std;class Integer{public:    Integer(int n);    ~Integer();    Integer &operator++();    Integer operator++(int n);    operator int();    void Display() const;private:    int n_;};Integer::Integer(int n) : n_(n) { }Integer::~Integer() { }Integer &Integer::operator ++(){    ++n_;    return *this;}Integer Integer::operator++(int n){    Integer tmp(n_);    n_++;    return tmp;}Integer::operator int(){    return n_;}void Integer::Display() const{    cout << n_ << endl;}int add(int a, int b){    return a + b;}int main(void){    Integer n(100);    n = 200;    n.Display();    int sum = add(n, 100);    cout << sum << endl;    int x = n;    int y = static_cast<int>(n);    return 0;}

Running result:

200

300

Explanation: Where n = 200, int is implicitly converted to Interger class; int x = n; it is used to call operator int to convert Interger class to int, or static_cast; in addition, when the add function transmits parameters, operator int is also called for conversion.


Iii.-> Operator Overloading

(1) The difference between "->" and "." in C ++

-> Is the operator that the Pointer Points to its members. It is the Member operator of the struct.


struct A{   int a;   int b;};A *point = malloc(sizeof(struct A));point->a = 1;A object;object.a = 1;


(2) class * operator-> (); and Class & operator *();

#include <iostream>using namespace std;class DBHelper{public:    DBHelper()    {        cout << "DB ..." << endl;    }    ~DBHelper()    {        cout << "~DB ..." << endl;    }    void Open()    {        cout << "Open ..." << endl;    }    void Close()    {        cout << "Close ..." << endl;    }    void Query()    {        cout << "Query ..." << endl;    }};class DB{public:    DB()    {        db_ = new DBHelper;    }    ~DB()    {        delete db_;    }    DBHelper *operator->()    {        return db_;    }    DBHelper &operator*()    {        return *db_;    }private:    DBHelper *db_;};int main(void){    DB db;    db->Open();    db->Query();    db->Close();    (*db).Open();    (*db).Query();    (*db).Close();    return 0;}
Running result:
DB...
Open...
Query...
Close...
Open...
Query...
Close...
~ DB...


Explanation: db-> Open (); is equivalent to (db. operator-> ()-> Open (); will call operator-> return DBHelper class pointer, call DBHelper member function Open (). The advantage of using this method is that you do not need to know when the db object needs to be released. When the lifetime ends, the analytic functions of the DB class are called, in which the delete db _; therefore, DBHelper class destructor will also be called.

(* Db). Open (); equivalent to (db. operator * (). Open ();


Refer:

C ++ primer version 4

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.