构造函数以及拷贝构造函数区别以及explicit的对比
2016-11-03 20:22
375 查看
#include <iostream>
#include <string>
using
namespace std;
class Str
{
public:
explicit Str(string cc
= "josan")
{
cout <<
"f1 iscalled\n";
str = cc;
}
explicit Str(Str& s)
{
cout <<
"f2 iscalled\n";
str = s.str;
}
void print();
~Str()
{
cout <<
"Bye\n";
}
/*const Str& operator=(const Str& ss);*/
private:
string str;
};
void Str::print()
{
cout << str
<< endl;
}
//const Str& Str::operator=(const Str& ss){
// cout <<"f3 is called\n";
// if (this ==&ss){
// return*this;
// }
// this->str =ss.str;
// return *this;
//}
int main()
{
{
//Str t;
//Str t();
//Str t{};
//Str t{ "szs" };
//Str t = { "szs" };
//Str t = Str{ "szs" };
//Str t("szs");
//Str t = "szs";
Str t = Str("szs");
4000
//t.print();
//Str* st = new Str;
//Str* st = new Str{};
//Str* st = new Str();
//Str* st = new Str{ "szs" };
//Str* st = new Str("szs");
//st->print();
}
cin.get();
return
0;
}
以上是程序的测试代码主要考察构造函数以及复制构造函数的区别与联系。同时,加入explicit关键词的比较。通过测试代码,可以更好地理解上述概念。
测试环境为VS2013C++
下面的表格就是各个测试结果。
备注:空格为无关 ⅹ表示无法通过 √表示存在调用关系 √+Str(Str& s)
表示构造以及拷贝构造同时存在调用
备注:空格为无关 ⅹ表示无法通过 √表示存在调用关系 √+Str(Str& s)
表示构造以及拷贝构造同时存在调用
#include <string>
using
namespace std;
class Str
{
public:
explicit Str(string cc
= "josan")
{
cout <<
"f1 iscalled\n";
str = cc;
}
explicit Str(Str& s)
{
cout <<
"f2 iscalled\n";
str = s.str;
}
void print();
~Str()
{
cout <<
"Bye\n";
}
/*const Str& operator=(const Str& ss);*/
private:
string str;
};
void Str::print()
{
cout << str
<< endl;
}
//const Str& Str::operator=(const Str& ss){
// cout <<"f3 is called\n";
// if (this ==&ss){
// return*this;
// }
// this->str =ss.str;
// return *this;
//}
int main()
{
{
//Str t;
//Str t();
//Str t{};
//Str t{ "szs" };
//Str t = { "szs" };
//Str t = Str{ "szs" };
//Str t("szs");
//Str t = "szs";
Str t = Str("szs");
4000
//t.print();
//Str* st = new Str;
//Str* st = new Str{};
//Str* st = new Str();
//Str* st = new Str{ "szs" };
//Str* st = new Str("szs");
//st->print();
}
cin.get();
return
0;
}
以上是程序的测试代码主要考察构造函数以及复制构造函数的区别与联系。同时,加入explicit关键词的比较。通过测试代码,可以更好地理解上述概念。
测试环境为VS2013C++
下面的表格就是各个测试结果。
| Str t; | Str t(); | Str t{}; | Str t{"szs"} | Str t={"szs"}; | Str t=Str{"szs"} | Str t("szs") | Str t = "szs"; | Str t = Str("szs"); |
Str(string cc = "josan") | √ | ⅹ | √ | √ | √ | √+Str(Str& s) | √ | √ | √+Str(Str& s) |
explicit Str(string cc = "josan") | √ | ⅹ | √ | √ | ⅹ | √+Str(Str& s) | √ | ⅹ | √+Str(Str& s) |
| | | | | | | | | |
Str(Str& s) | | ⅹ | | | | √ | | | √ |
explicit Str(Str& s) | | ⅹ | | | | ⅹ | | | ⅹ |
表示构造以及拷贝构造同时存在调用
| Str* st = new Str; | Str* st = new Str{}; | Str* st = new Str(); | Str* st = new Str{ "szs" }; | Str* st = new Str("szs"); |
Str(string cc = "josan") | √ | √ | √ | √ | √ |
explicit Str(string cc = "josan") | √ | √ | √ | √ | √ |
| | | | | |
Str(Str& s) | | | | | |
explicit Str(Str& s) | | | | | |
表示构造以及拷贝构造同时存在调用
相关文章推荐
- c#只读字段和常量的区别,以及静态构造函数的使用
- c#自定义类型的转换方式operator,以及implicit(隐式)和explicit (显示)声明的区别
- 拷贝构造函数,深拷贝,大约delete和default相关业务,explicit,给定初始类,构造函数和析构函数,成员函数和内联函数,关于记忆储存,默认参数,静态功能和正常功能,const功能,朋友
- c++中构造函数初始化的方法以及主要区别
- 八、构造函数和析构函数(二) 转换构造函数、赋值和初始化的区别、explicit
- 定义和赋值的区别 构造函数和拷贝构造函数
- C++中string类的处理字符串构造函数用法, 以及getline cin.getline()输入的区别
- 十、构造函数和析构函数(四) 拷贝构造函数、默认拷贝构造函数、拷贝构造函数调用几种情况、深拷贝浅拷贝、构造函数和=操作符区别、禁止对象拷贝
- MySQL与MongoDB的操作对比,以及区别
- String a=1与String a=new String("1")的区别,以及其他包装类类似对比
- const用法总结以及构造函数、析构函数、拷贝构造函数的深入剖析
- java中ArrayList 、LinkList区别以及速度对比
- c#自定义类型的转换方式operator,以及implicit(隐式)和explicit (显示)声明的区别
- C 语言中的左值和右值。以及对比数组名和指针取数组元素的区别。
- c++中构造函数初始化的方法以及主要区别
- lighttpd和nginx对比以及Nginx、Lighttpd与Apache的区别
- 构造函数而后拷贝构造函数的区别
- 赋值构造函数和拷贝构造函数的区别
- MySQL与MongoDB的操作对比,以及区别
- MySQL与MongoDB的操作对比,以及区别