构造函数与析构函数
来源:互联网 发布:淘宝云客服工作时间 编辑:程序博客网 时间:2024/05/01 15:58
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class B
{
private:
int data;
public:
B()
{
cout<<"default constructor"<<endl;
}
~B()
{
cout<<"destructed"<<endl;
}
B(int i):data(i)
{
cout<<"constructed by parameter"<<data<<endl;
}
};
B Play(B b)
{
return b;
}
int main()
{
B temp=Play(5);
return 0;
}
(1)该程序的输出结果是什么?为什么会有这样的输出?
(2)B(int i):data(i),这种用法的专业术语叫什么?
(3)Play(5),形参类型是类,而5是个常量,这样写合法吗?为什么?
答案:(1)constructed by parameter5
destructed
destructed
(2)带参数的构造函数,冒号后面是成员变量初始化列表
(3)合法,单个参数的构造函数如果不添加explicit关键字,会定义一个隐含的类型转换(从参数的类型转换为自己);添加explicit关键字会消除这种隐含转换。
- 构造函数与析构函数
- 构造函数与析构函数
- c#构造函数与析构函数
- 构造函数与析构函数
- 构造函数与析构函数(整理)
- 构造函数与析构函数
- 构造函数与析构函数
- c#构造函数与析构函数
- 构造函数与析构函数研究
- 构造函数与析构函数(读书笔记)
- 构造函数与析构函数
- 构造函数与析构函数
- 构造函数与析构函数
- 构造函数与析构函数
- C++ 构造函数与析构函数
- 构造函数与析构函数
- 构造函数与析构函数
- 使用构造函数与析构函数
- Oracle表分区
- Android 打包成APK
- 搭建Struts2开发环境– Struts2应用的配置文件
- spring IOC 简易实现
- ubuntu 如何安装rpm包(转化成deb)和解压命令
- 构造函数与析构函数
- Boot Time
- 语言1
- Java程序发送简单邮件
- 【流式计算】twitter storm Rationale[1]
- 语言2
- Windows下安装QT并运行第一个程序
- 扬创2440 uboot移植(1)--基于《嵌入式Linux之我行--u-boot-2009.08在2440上的移植详解》
- 最长公共子序列