c++ - Difference between two ways of using default constructor -


What is the difference between defining an object of a square "person" by using its default constructor after two different ways: / P>

Method 1:

  person = person ();  

Method 2:

  person ();  

When I started some variables inside the default constructor and tried to use those variables or tried to set those variables with the set / set methods in the main routine, then I found the compilation error in Method 2, but method 1 works.

Thank you.

  person = person ();  

This declares an person object called a person . It starts with this object a temporary object created by Person () . This means that it will implement the copy / driver converter of the person (which will probably be omitted).

  person ();  

This is a function is called person which gives the person object to an object There is no announcement.

You want it instead:

  person;  

This declares an object called person by calling person which is created by default.

You have introduced the new "Uniform" initialization syntax which is introduced in C ++ 11, which avoids the ambiguity between the variables and the announcements of the functions:

  Person person {};  

And finally, if you have died, you may want to use the style proposed by Herb Sutter:

  auto person = person {};  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -