Effective C + + clause 39

Source: Internet
Author: User

I have learned from these terms:
1.private inheritance differs from two other types of inheritance, and derived class objects cannot be implicitly converted to base class objects. The following code:

class Bird//鸟{};class ostrich:private Bird//鸵鸟{};int main(){Bird *b =  new ostrich();//编译不通过,基类不能转换为派生类}

The compiler explicitly states that the base class is inaccessible, so the conversion fails. Private inheritance has no so-called inheritance between the base class and the derived class, and its existence is purely for application-level service, that is, the inheritance relationship for the flexibility of programming.

2. The following code tells us the benefits of private invocation, and when there is a special case where the base class size is empty, calling private inheritance can minimize the amount of space the derived class occupies. Then someone would say that my defined class is not empty, why size is 1 bytes, here is a simple explanation. The size of any one class is not empty, even if you define
Class m{}; The size of the classes is still 1, the size of the class is only related to variables, and variables are non-static variables, and static variables do not affect the size of the class.

#include <string>#include <iostream>using namespace STD;classBird//Bird{ Public:Static stringNameStatic stringGetName () {returnName }};classOstrich:PrivateBird//Ostrich{};intMain () {intM1 =sizeof(Bird);//m1 size is 1 bytes    intM2 =sizeof(ostrich);//m2 size is also 1 bytes}

Effective C + + clause 39

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.