Prohibition of inheritance in C ++

xiaoxiao2021-03-06  52

The following code will prohibit inheritance:

Class A;

Class Lock {Friend Class A; PRIVATE: LOCK () {}};

Class A: Virtual Public Lock {// ... public: a () {} a (int T) {}

}

Now, if you try to get other classes from class A will get compilation errors like the following.

Class B: Public a {}; // Lock :: Lock ': Cannot Access Private Member Declared In Class' Lock'

This is because derived classes need to call the virtual base class constructor, so the derived B is required to call the virtual base class "" "also LOCK

The constructor is functional), and the constructor of the Lock is private, class B is not a Lock's friend, so this will generate a compilation error. If we remove the Virtual keyword when class A derived, the program will successfully compile. This is because any class can be in the non-virtual inheritance.

Call the constructor in the direct parent class. Therefore, in the non-virtual inheritance, B will call its direct parent class A and a direct father calling it.

The constructor of the class LOCK is legal.

转载请注明原文地址:https://www.9cbs.com/read-116475.html

New Post(0)