C + + notes: Derived classes to base class conversions to accessibility __c++

Source: Internet
Author: User
C + + notes: Derived classes to base class conversions to accessibility

to determine whether a conversion to a base class is accessible, you can consider whether the public member of the base class is accessible and, if so, that the conversion is inaccessible, otherwise it can be accessed. If it is public inheritance, both user code and descendant classes can use the conversion from the derived class to the base class. If a class is derived by using private or protected inheritance, user code cannot convert a derived type object to a base class object. In the case of private inheritance, a class derived from a private inheriting class cannot be converted to a base class. In the case of protected inheritance, members of subsequent derived classes can be converted to base class types. Regardless of the derived access label, the derived class itself can access the public members of the base class, so the members and friends of the derived class itself can always access the conversion of the derived class to the base class.

Like a inherited member function, the conversion from derived to base may or May is accessible. Whether The conversion is accessible depends on the access label specified on the derived class ' derivation.

If The inheritance is public, then both user code and member functions the subsequently derived -to-base conversion. If A class is derived using private or protected inheritance, then user code could not convert an object of derived type to A base type object. If The inheritance is private, then classes derived from the privately of class may isn't convert to the base class. If the inheritance is protected, then the members of subsequently derived classes may convert to the base type.

Regardless of the Derivation Access label, a public member of the base class are accessible to the derived class itself. Therefore, the derived-to-base conversion is always accessible to the members and friends of the derived class itself.

Tips:to determine whether the conversion to base are accessible, consider whether a public member of the base class would b E accessible. If So, the conversion is accessible; Otherwise, it is not. Test Code:

Class a{};
    Class B:public a{public:void Fun (b&obj) {A obj1 = (a) obj;

}
};
    Class c:protected a{public:void Fun (c&obj) {A obj1 = (a) obj;

}
};
    Class D:private a{public:void Fun (d&obj) {A obj1 = (a) obj;

}
};
    Class E:public b{public:void Fun (b&obj) {A obj1 = (a) obj;

}
};
    Class F:public c{public:void Fun (c&obj) {A obj1 = (a) obj;

}
};
A class derived from a private inheriting class cannot be converted to a base class.  Class H:public d{public:void Fun (d&obj) {A obj1 = (a) obj; Error C2247: "A" is not accessible because "D" Inherits//error C2243 from "a" using "Private": "Type Conversion": Conversion from "D *" to "Const A &" exists

, but cannot access}};
     
    void Inherite_test () {A *pb, *pc, *pe, *PD, *PF, *ph; PB = new B; Public PC = new C; Protected error C2243: "Type conversion": Conversion from "C *" to "A *" exists, but cannot access PD = new D; Private error C2243: "Type conversion": Conversion from "D *" to "A *" exists, but cannot access PE = new E; PubLic + Public PF = new F; Protected + Public Error C2243: "Type conversion": Conversion from "F *" to "A *" exists, but cannot be accessed ph = new H; Private + Public error C2243: "Type Conversions": Conversion from "H *" to "A *" exists but cannot be accessed}

parsing:

The first thing to understand is a few concepts: User code, descendant class (subsequently derived classes), derived class (derived class) user code, which refers to code other than friend functions, member functions. Descendant classes, not just the first-level derived classes, but also subsequent derived classes that are indirectly derived from the base class. Derived classes, which refer specifically to inheriting classes directly.


The class b,c,d are directly inherited from a by public,protected,private. The class E,f,h is inherited from the B,c,d class, respectively.

A transformation operation that derives classes to the base class in a member function fun.

1, PB = new B; B::fun,e::fun Function Description:

If it is public inheritance, both user code and descendant classes can use the conversion from the derived class to the base class.

If The inheritance is public, then both user code and member functions the subsequently derived -to-base Conversion

2, the failure of the C,d,f,h class conversion in Inherite_test explains:

If a class is derived by using private or protected inheritance, user code cannot convert a derived type object to a base class object.

If A class is derived using private or protected inheritance, then user code could not convert an object of derived type to A base type object.

3, H class Fun function error display:

A class derived from a private inheriting class cannot be converted to a base class.

If The inheritance is private, then classes derived from the privately of class may isn't convert to the base class.

4, F::fun function Description:

In the case of protected inheritance, members of subsequent derived classes can be converted to base class types.

If the inheritance is protected, then the members of subsequently derived classes may convert to the base type.

One thing to note: For multilevel derivations, multiple access labels are combined to look at accessibility. There is an easy way to see the tips above.

For example, a class C protected inherits from a, then the public member in a becomes protected in C, and class F public inherits from C, so that the public member of a in F, fun function is protected, is visible.

So it is correct to convert the derived class to the base class in F::fun.

However, in user code, it is not possible to access the public member of a protected in c,f, so the C,f object converts to the object of Class A error.


Reference:

http://blog.csdn.net/shanki_pm/article/details/6534222

Http://www.cnblogs.com/qingxinlangjing/p/3214472.html

http://blog.csdn.net/geekwangminli/article/details/7930853

From:

http://blog.csdn.net/liufei_learning/article/details/21587085

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.