C + + constructor initialization order detailed _c language

Source: Internet
Author: User
Tags inheritance

1. Introduction to constructors, destructors and copy constructors

Constructors

1. Constructors cannot have return values
2. When the default constructor is invoked, the default constructor is automatically called to initialize the object, and the default constructor initializes all data members to zero or null
3. When an object is created, the system automatically calls the constructor

destructor

1. The destructor has no parameters and no return value. Cannot overload, that is, only one destructor can be defined in a class
2. If a destructor is not defined in a class, the system automatically generates a default destructor, an empty function, and nothing.
3. Conditions of invocation: 1. An object defined in the body of a function that is called automatically by the destructor of the class at the end of the function, 2. An object that is built dynamically with the new operator when it is released using the delete operator.

Copy Constructors

A copy constructor is actually a constructor, with all the attributes of a generic constructor, with the same name as the owning class name. There is only one parameter in the copy constructor, which is a reference to a homogeneous object. It is invoked in three different cases:

1. Initializes another object of the class with a known object of the class;
2. The formal parameter of a function is the object of the class, and the function is invoked to combine the formal parameter and the actual parameter.
3. The return value of the function is the object of the class, and the function performs the return of the caller.

Code

Copy Code code as follows:

/*
version:1.0
Author:hellogiser
Date:2014/9/25
*/

#include "stdafx.h"
#include <iostream>
using namespace Std;

Class Point
{
Private
int x, y;
Public
Point (int xx = 0, int yy = 0)
{
x = XX;
y = yy;
cout << "constructor" << Endl;
}
Point (const point &p)
{
x = p.x;
y = p.y;
cout << "Copy constructor" << Endl;
}
~point ()
{
cout << "destructor" << Endl;
}
int get_x ()
{
return x;
}
int get_y ()
{
return y;
}
};


void f (Point P)
{
Copy constructor
cout << p.get_x () << "" << p.get_y () << Endl;
destructor
}

Point G ()
{
Point A (7, 33); Constructor
return A; Copy Constructor Temp Object
}

void Test ()
{
Point A (15, 22); Constructor
Point B (a); (1) Copy constructor
cout << b.get_x () << "" << b.get_y () << Endl; 15 22
f (b);//(2) copy constructor
b = g (); (3) Copy constructor
cout << b.get_x () << "" << b.get_y () << Endl; 7 33
}

int main ()
{
Test ();
return 0;
}
/*
Constructor
Copy Constructor
15 22
Copy Constructor
15 22
destructor
Constructor
Copy Constructor
destructor
destructor
7 33
destructor
destructor
*/

2. Constructor execution order in inheritance relationship

(1) The constructors of any virtual base class (dummy) are constructed in the order in which they are inherited;
(2) The constructors of any Non-virtual base class (non-virtual) are constructed according to the order in which they are inherited;
(3) The constructors of any member object (data) are invoked in the order in which they are declared;
(4) class own constructor (self).

Code

Copy Code code as follows:

/*
version:1.0
Author:hellogiser
Date:2014/9/27
*/

#include "stdafx.h"
#include <iostream>
using namespace Std;


Class OBJ1
{
Public
OBJ1 ()
{
cout << "obj1\n";
}
};

Class OBJ2
{
Public
OBJ2 ()
{
cout << "obj2\n";
}
};

Class Base1
{
Public
Base1 ()
{
cout << "base1\n";
}
};

Class Base2
{
Public
Base2 ()
{
cout << "base2\n";
}
};

Class Base3
{
Public
BASE3 ()
{
cout << "base3\n";
}
};

Class Base4
{
Public
Base4 ()
{
cout << "base4\n";
}
};

Class Derived:public Base1, virtual public Base2,
Public Base3, virtual public Base4
{
Public
Derived (): Base4 (), Base3 (), Base2 (),
Base1 (), Obj2 (), Obj1 ()
{
cout << "derived.\n";
}
Protected
OBJ1 obj1;
OBJ2 Obj2;
};

void Test ()
{
Derived AA;
cout << "This is ok.\n";
}

int main ()
{
Test ();
return 0;
}
/*
Base2
Base4
Base1
Base3
OBJ1
OBJ2
Derived.
This is OK.
*/

"Code 2"

Copy Code code as follows:

/*
version:1.0
Author:hellogiser
Date:2014/9/27
*/

#include "stdafx.h"
#include <iostream>
using namespace Std;

Class Base1
{
Public
BASE1 (int i)
{
cout << "Base1" << i << Endl;
}
};

Class Base2
{
Public
BASE2 (int i)
{
cout << "Base2" << i << Endl;
}
};

Class Base3
{
Public
BASE3 ()
{
cout << "Base3 *" << Endl;
}
};

Class Derived:public Base2, public Base1, virtual public Base3
{
Public
Derived (int A, int b, int c, int d, int e)
: Base1 (a), B2 (d), B1 (c), Base2 (b)
{
m = e;
cout << "derived.\n";
}
Protected
BASE1 B1;
Base2 B2;
BASE3 B3;
int m;
};

void Test ()
{
Derived AA (1, 2, 3, 4, 5);
cout << "This is ok.\n";
}

int main ()
{
Test ();
return 0;
}
/*
BASE3 *
Base2 2
Base1 1
Base1 3
Base2 4
BASE3 *
Derived.
This is OK.
*/

Analysis:

(1) virtual

In order of inheritance: BASE3

Step one: First inherit Base3, Base3 () is not found in the initialization list, then call the default constructor in Base3 Base3 (), print "Base3 *"

(2) Non-virtual

In order of inheritance: Base2,base1

Step two: Inherit Base2, find Base2 (b) In the initialization list, invoke Base2 constructor Base2 (2), print "Base2 2"

Step three: Inherit Base1, find Base1 (a) in the initialization list, call Base1 constructor Base1 (1), print "Base1 1"

(3) data member

In order of declaration: B1,B2,B3

Step Fourth: Construct B1, find B1 (c) In the initialization list, invoke Base1 constructor Base1 (3), print "Base1 3"

Step Fifth: Construct B2, find B2 (d) In the initialization list, call Base2 constructor Base1 (4), print "Base2 4"

Step Sixth: Construct B3, B3 () is not found in the initialization list, call Base3 constructor Base3 (), print "Base3 *"

(4) Self

Step 7th: Execute your own constructor body and output "Derived."

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.