1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- //4、2、4 构造函数的调用规则
- //默认情况下,C++至少给一个类添加3个函数
- //?默认构造函数(无参,函数体为空)
- //?默认析构函数(无参,函数体为空)
- //?默认拷贝构造函数,对属性进行值拷贝
- //构造函数调用规则如下:
- //?如果用户定义有参构造函数,C++不再提供无参构造函数,但是会提供默认拷贝构造
- //?如果用户定义拷贝构造函数,C++不会再提供其他构造函数
- #if(0)
- #include <iostream>
- class Person
- {
- public:
- //Person()
- //{
- // std::cout << "Person的默认构造函数调用" << std::endl;
- //}
- Person(int age)
- {
- std::cout << "Person的有参构造函数调用" << std::endl;
- m_Age = age;
- }
- Person(const Person& p)
- {
- std::cout << "Person的拷贝构造函数调用" << std::endl;
- }
- ~Person()
- {
- std::cout << "Person的析构函数调用" << std::endl;
- }
- int m_Age;
- };
- //void test01()
- //{
- // Person p;
- // p.m_Age = 18;
- //
- // Person p2(p);
- //
- // std::cout << "p2的年龄为:" << p2.m_Age << std::endl;
- //
- //}
- void test02()
- {
- Person p(18);
- Person p2(p);
- }
- int main()
- {
- //test01();
- test02();
-
- system("pause");
- return 0;
- }
- #endif
|