C # type operator overloading call Test in class inheritance

Source: Internet
Author: User

This is an obscure one-sided study, a simple hierarchy of inheritance
    classCA {}classcb:ca{}classcc:cb{}}voidTest (CA OA) {//catestDebug.Log ("ca==============="); }    voidTest (CB OA) {//cbtestDebug.Log ("cb==============="); }    voidTest (CC OA) {//cctestDebug.Log ("cc==============="); }//The test code is as follows:CC OC =NewCC ();    Test (OC); 

In this case, the call to test (OC) has the following rules:< by commenting out other functions for testing >

    1. If Catest, Cbtest, cctest Three overloaded functions are present, test (OC) will call Cctest
    2. If only catest, cbtest two overloaded functions, test (OC) will call Cbtest
    3. If only one of the three functions exists, then test (OC) calls the function.

From this we know that when the Test (OC) is called, the compiler will match the parameters of the overloaded function from the inheritance hierarchy of OC to the priority of the parent. This is also in line with normal logic.

Second, the inheritance of operator overloading in a class
     classCA {}classcb:ca{}classcc:cb{/*Requirements for the MSDN type Conversion 1. The operand must be of the enclosing type 2. The type conversion definition of Class A to Class B cannot be performed in Class C (that is, the conversion of 2 classes cannot be defined in class 3rd, as defined in the following parameter)*/         Public Static Implicit operator BOOL(CC OT/*cannot be ca ot or CB ot*/) {Debug.Log ("bool================"); returnOT! =NULL; }    }    voidTest (CA OA) {//catestDebug.Log ("ca==============="); }    voidTest (CB OA) {//cbtestDebug.Log ("cb==============="); }    voidTest (CC OA) {//cctestDebug.Log ("cc==============="); }
   void Test (bool b) {//booltest
Debug.Log ("b===============")
}
//The test code is as follows:CC OC =NewCC (); Test (OC);

In this case, booltest overloaded functions and Catest, cbtest, and any of the overloads of Cctest, are conflicting for the following reasons:

When test (OC) is called, the compilation system matches the parameters of the four overloaded functions of OC with Test, but finds that four matches the successful one. Test (bool b) is matched by an overload of the bool type character of the CC class.

Catest, Cbtest, cctest three overloaded functions because the formal parameter cc,cb,ca is an inheritance relationship, there is a priority in the match, because OT is the CC type, so the priority cc>cb>ca, so there is no conflict between the three overloaded functions, The compiler explicitly knows which overload to call. The bool overload and CC,CB,CA are the same priority when the type is converted, so the compilation system does not know whether to call the bool overload or the three overloads of the Cc,cb,ca.

If you move the bool overload from class CC to the class CA, the other code does not change and the test code does not change. Tested, Booltest,catest, Cbtest, cctest Four overloads can coexist, that is, Booltest does not conflict with any other overloads.

    1. Booltest, catest, cbtest, cctest exist simultaneously, Test (OC) calls the Cctest
    2. Booltest, Catest, cbtest exists at the same time,Test (OC) calls the cbtest
    3. Booltest, Catest exists at the same time,Test (OC) calls the catest
    4. Booltest, catest, Cbtest, Cctest only one exists , then call this existence
    5. This overloaded function is also called when only booltest exists.

This shows that the invocation priority of the type overloading operator of the base class is lower than the priority of the parent-child-level conversion, as in Scenario 5, which is called only if the bool overloaded operator is available .

The precedence of type overloaded operators for this class equals the precedence of parent-child hierarchy conversions.

C # type operator overloading call Test in class inheritance

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.