A class is designed in C + + so that it cannot inherit

Source: Internet
Author: User

The final keyword can be used directly in 1.C11 so that the class cannot be inherited

2. The derived class initializes the base class part by calling the constructor of the base class, and if the base class constructor is declared private, the link error occurs when the derived class member is instantiated, but such a base class cannot be instantiated, and the compilation error

3. The constructor of a class is declared private, and the class and its subclasses cannot be instantiated, but if the handle class is declared as a friend of the class, the subclass can call the private member function of the base class, that is, the subclass can be instantiated at this time, and subclasses of subclasses can also be initialized. Because subclasses of subclasses initialize the base class part of the constructor of the calling subclass, and the subclass is normal initialization, so think of virtual inheritance, subclass virtual inheritance from the parent class, subclasses of subclass normal inheritance from the child class, at this time the subclass and subclass subclass a pointer, the pointer to the base class part.

class a{private:    A () {}    class  B;}; class Virtual a{}; class  Public b{}; void Main () {    b b; // OK    c C; // false}

A class is designed in C + + so that it cannot inherit

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.