C ++ learning (c ++ 17)-parent class and constructor, destructor

Recently, the main course is quite heavy, plus the basis of reviewing the class and object, there is not much need for reStudy, so there is not much to remember about the blog. Today I saw the use of the parent class, I feel that this piece has always been vague, still have to remember.



LeoRanbom's blog

Original post address : https://www.cnblogs.com/ranbom/

Blogger LeoRanbom

Only posted on the blog of the original post address, all other places are crawled.

If you think it's good, I hope you like it.


Parent class and constructor

When creating an object, the parent class and the objects contained in it are created at the same time.

The order of execution is roughly as follows:

  1. First execute the parent class constructor
  2. Then execute the constructor of the data member object
  3. Finally, the constructor of the class itself is executed.

But if you have real knowledge in practice, it is still a bit deeper.

#include<iostream>
#include<vector>
#include<algorithm>
#include<random>
#include<chrono>
using namespace std;

class Something{
public:
    Something() { cout << 2; }
};

class Based {
public:
    Based() {
        cout << 1;
    }
};
class Derived : public Based {
public:
    Derived() { cout << 3; }
private:
    Something mSomething;
};
int main() {
    Derived derived;
}

The result is output 123. meets the.

If the parent class constructor has parameters, you can use the initializer next to the child class constructor to link the constructor.

Seeing my little head here, I have a big doubt-isn't the parent class constructor executed first? Is it useful for the subclass constructor to pass to the parent class initializer? Nothing is written in the book, just knock it.

class Something{
public:
    Something() { cout << 2; }
};

class Based {
public:
    Based(int i) {
        cout << i;
    }
};
class Derived : public Based {
public:
    Derived():Based(7){ cout << 3; }
private:
    Something mSomething;
};
int main() {
    Derived derived;
}

As above, I changed the constructor of the parent Based to the parameterized constructor, and then used the initializer in the constructor of the child class Derived to pass parameters to the parent Based. The output is 723. Okay, probably understand how it is an output law.

Parent class calls methods overridden by subclasses

There are new doubts, try it.

Ordinary rewriting & virtual methods

class Something{
public:
    Something() { cout << 2; }
};

class Based {
public:
    Based() {
        pushout();
    }
    void pushout() { cout << 'a'; }
};
class Derived : public Based {
public:
    Derived() { pushout(); }
    void pushout() { cout << 'b'; }
private:
    Something mSomething;
};
int main() {
    Derived derived;//Based* ptr = new Derived同样的效果,输出a2b
}

Output a2b, that is, the parent class constructor will be called when the subclass object is created, and the parent class method will be executed in the constructor.

How about changing to a virtual method?

Tried it and also output a2b. That is, whether it is an ordinary method rewrite or virtual method rewrite, it will call the method of the parent class.

If the subclass is not overridden, the method of the parent class is output. a2a

I tried the constructor virtual for a while, and the compiler reported that the constructor only allowed inline types.

Parent class and destructor

First throw order:

  1. Destructor of this class
  2. Destroy the data, so next is the destructor of the data member
  3. Parent destructor

Contrary to the order of the constructor, easy to remember.

Contrary to the constructor that cannot be virtual, the destructor is recommended! Suggest! the best! Plus virtual.

class Something{
public:
    Something() { cout << 2; }
    ~Something() { cout << 2; }
};

class Based {
public:
     Based() {
         cout << 1;
    }
     ~Based() { cout << 1; }
};
class Derived : public Based {
public:
    Derived() { cout << 3; }
    ~Derived() { cout << 3; }
private:
    Something mSomething;
};

int main() {
    Based* ptr = new Derived;
    delete ptr;
}

This code destructor does not add virtual, and finally outputs 1231, that is, the destructor of the subclass and data member is not called, which is a serious bug.

And after adding, it will output 123321.

Call the original method of the parent class in the subclass rewriting method

There is no way to run the code directly. Checked it because C ++ 's name resolution rules parse local scope first, then class scope. So the subclass will call itself non-stop, forming infinite recursion. If you want to call this method of the parent class, you should call it with Based :: pushout ().

Found that the __super keyword is supported in Visual C ++ (2 underbars)

You can use __super :: method name to refer to the parent method. (It seems that this is also true for java?)

Guess you like

Origin www.cnblogs.com/ranbom/p/12758076.html