B is inherited from a. pointer A cannot be implicitly converted to pointer B. function parameters only support pointer type and are irrelevant to objects actually pointed.

Source: Internet
Author: User

# Include
<Iostream>

# Include
<String>

 

Using namespace STD;

 

Class

{};

 

Class B:
Public

{};

 

Void F (B & B)

{

Cout <"F (B & B)" <Endl;

}

Void F (A &)

{

Cout <"F (A & A)" <Endl;

}

 

Int _ tmain (INT argc, _ tchar * argv [])

{

A * P1 = new B;

F (* P1); // call f (A &)

 

A;

B * P2 = (B *) &;

F (* P2); // call f (B & B)

 

/* F (* P2) Call analysis: F (B & B) is equivalent to F (B * P), F (A & A) is equivalent to F (A * P ),

F (* P2) is equivalent to F (P2), while P2 is B * type. Of course, F (B * P) is called, that is, F (B & B) is called ).

The same applies to F (* P1.

It can be seen that the parameter type should be matched, regardless of the object actually pointed to (function parameters only support the pointer type ). */

 

System ("pause ");

Return 0;

}

 

However, if there is no void F (A & A) function, that is:

Class

{};

 

Class B:
Public

{};

 

Void F (B & B)

{

Cout <"F (B & B)" <Endl;

}

Int _ tmain (INT argc, _ tchar * argv [])

{

A * P1 = new B;

F (* P1); // compilation error. A * cannot be implicitly converted to B *. F (A * P) is supposed to be called, but only F (B * P) is required, and a * cannot be implicitly converted to B *. Therefore, F (B * P) is not called ), error.

 

System ("pause ");

Return 0;

}

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.