C + + constructor plus colon

Source: Internet
Author: User

In fact, the content after the colon is the initialization of the member list, there are generally three kinds of situations:
1. Initialize the object that contains the object member, for example,
Class line has two private object members StartPoint, Endpoint,line's constructor written:
Line (int sx,int sy,int ex,int ey): StartPoint (Sx,sy), endpoint (Ex,ey) {...}
The constructors of the respective objects are called separately in the order of the object members in the class definition, and then their constructors are executed.
2, for objects without object members, the initialization can also be applied to the above format, for example,
The class rectangle has two data members of length, width, and its constructors are written as:
Rectangle (): Length (1), Width (2) {}
Rectangle (int x,int y): Length (x), Width (y) {}
3. Initialize the parent class , for example,
The parent class of CDLGCALCDLG is the MFC class CDialog, whose constructor is written as:
Cdlgcalcdlg (cwnd* pparent): CDialog (Cdlgcalcdlg::idd, pparent)
Where IDD is an enumeration element, the ID of the flag dialog template
Initializing an object with an initialized member list is sometimes necessary, sometimes for efficiency reasons

http://ivan4126.blog.163.com/blog/static/209491092201301534928785/

C + + constructor plus colon

Related Article

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.