C ++ object model Learning Record (1)-Chapter 2nd constructor Semantics

Source: Internet
Author: User

1.

# Include <iostream>

Using namespace STD;

Class foo
{
Public:
Foo (), Foo (INT );
};
// The Bar default constructor synthesized here contains the Member
// Object, foo has default constructor, so bar: Foo () is initialized ()
// But it is the programmer's responsibility to initialize STR, that is, the compiler will not initialize Str

Class bar
{
Public:
Foo; // contains
Char * STR;
};
Void foo_BAR ()
{
Bar;

}
Int main ()
{
Cout <"Hello world! "<Endl;
Return 0;
}

Bar in the above Code may be converted

Bar: bar ()

{

// Pseudo code

Foo. Foo (): Foo ()

}

The merged default constructor only meets the needs of the compiler and does not meet the needs of the program. If the program is correct, the STR must be initialized.

Bar: bar () {STR = 0;}, with this custom constructor, the compiler cannot synthesize 2nd. At this time, the compiler takes:

"If Class A contains one or more member objects", each constructor of Class A must call the default constructor of every member classes.

If there are multiple Member objects, the compiler completes initialization in the declared order.

2. In four cases, the compiler will synthesize a default constructor for the class of the undeclared constructor.

(1) If a class does not have any constructor, but it contains a member object, this member object has a default constructor, the default constructor of implicit in this class is nontrivial, And the compiler combines it into a default constructor. However, this default constructor is only merged as needed.

How to Avoid merging multiple Default constructors? The compiler uses the inline function.

In fact, the default constructor, default destructor, and assignment copy operator are all completed in the internal connection mode, and the inline function is statically linked.

(2) base class with default constructor

If no constructor class inherits from a "class with constructor", the default constructor of the drived class subclass will be considered as a nontrivial and synthesized, if multiple constructors are defined in the subclass, but none of them use the default constructor of the base class, the compiler will extend each constructor of the subclass.

(3) class with virtual function

Class is declared as (or inherited from) a virtual function

Class is derived from an inherited string, with one or more virtual functions

(4) class with a virtual base class

Two Misunderstandings

1. If no default constructor is defined for any class, the compiler automatically creates

2. The default constructor synthesized by the compiler will explicitly set the default value for each data member in the class.

The compiler only sets the values required for compilation, and other default values must be initialized by the programmer.

# Include <iostream>
Using namespace STD;
Class string
{
// Provides the copy constructor that is not displayed in the class
public:
String(string name)
{
str = name;
}
string getStr()
{
return str;
}
private :
string str;
};

int main()
{
String noun("book");
String verb = noun;
cout << verb.getStr() << endl;
return 0;
}

  

3. Three situations of copy constructor Construction

1. Perform Explicit initialization on a class

2. When used as a function parameter

3. When the return value of a function (returning an object)

In addition, if there is a display implementation of the copy constructor, the compiler uses the so-called default memberwise initializator method to copy a built-in data member from one object to another, however, the member class object will not be copied. Instead, the default memberwise initializator method is used recursively.

An incorrect understanding:

For classes with undefined copy constructors, the compiler automatically implements a copy constructor.

C ++ standard divides the copy constructor into nontrivial and trivial. Only nontrivial can be merged into the program.

Rather than trivial, it depends on whether the class shows bitwise copy semantics (bit-by-bit copy)

4. When will the class not show bitwise successive copies?

1. When the class contains a member Class Object, there is a default copy constructor in this Member class.

2. When the class inherits from a base class, And the Bese class has a copy constructor

3. When a class declares multiple virtual functions

4. When the class is derived from an inherited string, one or more virtual base classes

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.