构造函数的分类

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Test2
 5 {
 6 public:
 7     Test2()
 8     {
 9 
10         cout << "无参构造函数" << endl;
11     }
12     Test2(int a)
13     {
14         m_a = a;
15         m_b = 0;
16     }
17     Test2(int a, int b)
18     {
19         m_a = a;
20         m_b = b;
21         cout << "有参构造函数" << endl;
22     }
23     //赋值构造函数 (copy构造函数) //
24     Test2(const Test2& t)
25     {
26         cout << "我也是构造函数 " << endl;
27     }
28     /*Test2(int a, int b, int c)
29     {
30     cout << "我是3参构造函数";
31     }*/
32 public:
33     void printT()
34     {
35         cout << "普通成员函数" << endl;
36     }
37 private:
38     int m_a;
39     int m_b;
40     int c;
41 protected:
42 };
43 
44 
45 int main()
46 {
47     //1括号法
48     Test2 t1(1, 2);    //调用参数构造函数,c++编译器自动的构造函数
49     t1.printT();
50     //2=号法
51     Test2 t2 = (1,2,3,4,5,6,7);    //逗号表达式    //c++ =对等符号 功能增强,c++编译器自动的构造函数
52     Test2 t3 = 5;
53     //3 直接调用构造函数  手动的调用构造函数
54     Test2 t4 = Test2(4, 2);    //匿名对象
55     t1 = t4; //把t4 copy给 t1  //赋值操作 
56     //对象的初始化 和 对象的赋值 是两个不同的概念 
57     cout<<"hello"<<endl;
58     system("pause");
59     return 0;
60 }
原文地址:https://www.cnblogs.com/linst/p/4913475.html