Understanding of C ++ object Creation

Source: Internet
Author: User

Class

{

Void SS (){**************};

}

Class A has the member function SS ();

The following operations can be performed:

A *;

A * B = NULL;

A * c = new ();

A-> SS ();

B-> SS ();

C-> SS ();

The access to SS () is successful. The reason is that SS () does not retrieve the member variables in Class A, or no member variables in Class A and SS () are not virtual functions.

When Class A is changed to the following:

Class

{

Int COUNT = 100;

Void SS (){*************};

}

The above operations can also be successful in the face of SS (), Because SS () does not retrieve the member variable in Class A or SS () is not a virtual function.

When Class A is changed to the following:

Class

{

Int COUNT = 100;

Void SS () {int CCC = count ;};

}

Only a * c = new A () can be retrieved from the above SS (); otherwise, the crash error may occur, because the new function is not used, the member variable count is not created (initialized), but is declared. Therefore, an error is reported during access. This is the same as the existence of virtual functions in Class A, because if Class A has virtual functions, Class A will save a pointer to the list of virtual functions, if it is not new, the function pointer cannot be initialized. Therefore, a crash error occurs, especially when you need to access a virtual function.

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.