Explict keywords in C + +

Source: Internet
Author: User

C + + stipulates that an implicit invocation is defined for a default constructor that may only pass one parameter.

Note: Simply passing a parameter refers not only to the default constructor for only one parameter, but also to those that contain the default constructor that defines the default value of the parameter.

To illustrate:

Class MyClass

{

Public

MyClass ();

MyClass (string s);

MyClass (int i, int y = 0);
}

Here the MyClass (string s) function and the MyClass (int i, int y = 0) function are the default constructors that may only be passed one parameter

Look at the case where the class object is constructed using constructors:

  • MyClass Obj1;//ok, called the MyClass ()
  • MyClass obj2 (String ("Hello world!")); /ok, called MyClass (string s)
  • MyClass obj3 (0),//ok, called MyClass (int i, int.
  • MyClass Obj4 (12);//Ibid.
  • MyClass obj5 = 12;//Ibid. But for Obj5, it is actually equivalent to MyClass temp (12); MyClass obj5 (temp);//means that the default copy constructor is called by default. This is known as implicit invocation.

In most cases, implicit invocation can cause bugs and is difficult to detect. Based on this, C + + introduces the Explict keyword to turn off this implicit invocation and avoid the bug caused by an implicit invocation.

Explict keywords in C + +

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.