Question about calling a C-language Constructor (interesting)

Source: Internet
Author: User
Class myclass001
{
PRIVATE:

Public:
Myclass001 ()
{
Printf ("myclass001 ");
}

Myclass001 (int I)
{
Printf ("MyClass001-i ");
}

};

Resolve:
Myclass001 K1; // display myclass001
K1 = 1; // display MyClass001-i

In fact, the second sentence is equivalent to calling the constructor with int I. In fact, it is assigning an I value to the constructor.
Thus, myclass001 (int I) is called ).
If you write a class test;
Class Test
{
Public int I;
}; Before myclass001.
Then change myclass001 (int I)
{
Printf ("MyClass001-i ");
}
Myclass001 (test * TT)
{
Printf ("MyClass001-test ");
}
Statement
Myclass001 K1;
K1 = new test;
Correct. It is obvious that new test is passed in as a parameter.

The parameter is not initialized when the object is declared, so the constructor without parameters is called to construct the object.
The value of the second row does not match the type on both sides of the equal sign. Therefore, the compiler does not find any constructor that uses the data type on the right as the parameter. After finding the constructor, a temporary object is constructed and then assigned a value.

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.