FAQ about C + + constructors

Source: Internet
Author: User

[1] What is a constructor used for?

Constructors construct objects of classes, initialize class variables, allocate resources (memory, files, semaphores, socket interfaces, and so on)

[2] list x; and list x (); What's the difference ?

The previous one is an object that defines the list, and the latter is a function that returns a list value

[3] is it possible for a class constructor to invoke another constructor to initialize itself?

To give an example of this problem:

class Foo {public:  foo (char  x);  Foo (charint  y);  ...}; Foo::foo (char  x) {  ...   0);  // This line does does help initialize the this object!!   ...}

Statement:



Just initializes a temporary object, not the This object, so the statement does not have the desired effect.

[4] Whether the default function function of the Fred class must be fred::fred ()?

Not necessarily, the default constructor refers to a constructor without parameters, for example: a::a (int x = 3, int y = 5)

Therefore, a class can not have a default constructor , which is important to note!

[5] If you want to create an Fred pair like an array, what number of constructor functions will be called?

For Fred a[10]; or Fred *a = new Fred (); If the class Fred has a default constructor, the default constructor is called 10 times, and if the class Fred does not have a default constructor, the compilation error occurs;

For Fred A[10] = {fred (5,7)}; The Fred (int, int) constructor is called;

[6] When a constructor initializes a member variable, should it use an "initialization list" or "assignment"?

initialization list;

Advantages: 1) Improve performance; 2)

[7] Is there a problem with the this pointer in the constructor?

[8] What is a "name constructor" (Named Constructor Idiom)?

[9] "value back" means an extra copy?

[10] Why can't we initialize a static member variable in the constructor initialization list?

[11] Why would a class with a static member variable have a link error?

[12] What is "static initialization order Fiasco"?

[13] should I avoid "static initialization order Fiasco"?

[14] Why construct-on-first-use static variables instead of pointers?

[15] How can I avoid "static initialization order fiasco" in statically members?

[16] Do I have to worry about the built-in type of "static initialization order Fiasco"?

[17] What if something goes wrong with the constructor?

[18] What is a "named parameter idiom" (Named Parameter Idiom)?

[19] Why do I get an error by declaring a Foo object with foo x (Bar ()) ?

FAQ about C + + constructors

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.