C ++ object-oriented basics, as well as some common interview correction questions.

Source: Internet
Author: User

1. constructor with default parameters (avoid using it in practice)

There will be ambiguity!

Class A {public: A (INT I = 10, Int J = 11, int K = 12); A (INT, INT); int I, j, k ;}; a: A (int A, int B, int c) {I = A; j = B; k = C;} A: A (int I, Int J) {This-> I = I; this-> J = J; this-> K = 100;} int main () {A; A A2 (20 ); A A3 (30,40); // error ambiguous call to overloaded function return 0 ;}

2. pointer to the object member function

Review the pointer variables of common functions:

INT (* p) (INT, INT );

The definition is divided into three parts: function type (* function pointer variable) (parameter Type 1, parameter type 2 ,......)

Void fun () {cout <"this is fun ()" <Endl ;}int main () {void (* p )(); // P is the pointer variable pointing to the void function p = fun; // assign the entry address of the fun function to the pointer Variable P, and P points to the function fun (* P) (); // call the fun function. Return 0 ;}

Defining a pointer variable pointing to an object member function is more complex.

An error occurred while writing it directly!

Class test {PRIVATE: int num; public: void getnum () {cout <num <Endl;} void setnum (INT num) {This-> num = num ;}}; int main () {test T; T. setnum (10); void (* p) (); // P is the pointer variable pointing to the void function p = T. getnum; // error! Error c2440: '=': cannot convert from 'void (_ thiscall test: *) (void) 'to 'void (_ cdecl *) (void) 'Return 0 ;}

Cause:

The most fundamental difference between a member function and a common function is that it is a member of a class. The compilation system requires that in the preceding copy statement, the pointer variable type must match the type of the function on the right of the value assignment.

Match in the following three aspects:

1) type and number of function parameters.

2) type of function return value

3) class.

3rd) Point mismatch! The following methods should be used:

This is really a pain! I know why Java is so popular, but I cannot afford to switch from Java to C ++.

Int main () {test T; T. setnum (10); // void (* p) (); // P is the pointer Variable Void (test: * p) () pointing to the void function )(); P = & test: getnum; // defines the public member function (T. * p) (); // call P to point to the test class public member function getnum (); 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.