C++ 有关赋值构造函数 拷贝构造函数 自动型别转换
来源:互联网 发布:java分布式系统有哪些 编辑:程序博客网 时间:2024/06/07 06:08
#include <iostream>using namespace std;class T{private:int num;public:T():num(0) {cout << "In T(): num = " << num << endl;}T(int size):num(size) {cout << "In T(int): num = " << num << endl; }T(const T &t) {cout << "In T(const T &)" << endl;}void setNum(int num) {this->num = num;}~T() {cout << "In ~T():num = " << num << endl; }T &operator = (const T &t) {cout << "In =" << endl;}};void f(T t) {cout << "In f" << endl;};int main(){f(10); //调用 T(int size)构造函数生成一个对象,这个对象在f函数结束时调用析构函数释放 T t;//调用 T()构造函数 t = 40;//先通过40自动型别转换调用T(int size)生成一个临时对象,然后调用T &operator = (const T &t)赋值构造函数,然后临时对象释放调用析构函数 t.setNum(12);T s = 88;//通过自动型别转换 用T(int size)生成s对象 cout << "aaaa" << endl;return 0;//调用s的析构函数,然后调用t的析构函数 }
输出:
In T(int): num = 10
In f
In ~T():num = 10
In T(): num = 0
In T(int): num = 40
In =
In ~T():num = 40
In T(int): num = 88
aaaa
In ~T():num = 88
In ~T():num = 12
- C++ 有关赋值构造函数 拷贝构造函数 自动型别转换
- C++:析构函数、对象生命周期、类型转换构造、拷贝构造、拷贝赋值、深浅拷贝
- 【C++】拷贝构造函数和赋值函数
- 拷贝,赋值构造函数赋值
- 构造函数 拷贝函数 赋值构造函数
- 构造函数 拷贝函数 赋值构造函数
- 构造函数、拷贝构造函数、赋值函数
- c++_7: 拷贝构造函数和赋值构造函数
- C ++ 拷贝构造函数和赋值构造函数 非常经典
- 赋值函数、拷贝构造函数
- 拷贝构造函数赋值函数
- 拷贝构造函数,赋值函数
- 拷贝构造函数和赋值构造函数
- 拷贝构造函数与赋值构造函数
- 拷贝构造函数与赋值构造函数
- C++ 拷贝构造函数 赋值构造函数
- 拷贝构造函数与赋值构造函数
- 拷贝构造函数和赋值构造函数
- html,css,js代码格式化,及其压缩
- dll与lib之间的区别
- perl ole接口 API演示介绍
- Java上路15-集合工具类
- Redis入门
- C++ 有关赋值构造函数 拷贝构造函数 自动型别转换
- Redis在Linux中的安装与启动
- HDOJ 1241 Oil deposits
- php 配置GD2.dll 和 php_curl.dll
- SBJson用法
- JSP————标签小结
- WP SDK7.1中文版+SDK7.1.1更新包安装方法
- HDOJ 1022 Train Problem I
- Fedora x64 17连接wpa加密的无线路由器