构造函数,拷贝构造的优化相关问题
2018-02-01 18:58
197 查看
话不多说,直接看题,先看简单一点的。
第一题:
#include<iostream>
using namespace std;
class Date
{
public:
Date()
{
cout << "Date()" << endl;
}
Date(const Date& d)
{
cout << "Date(const Date& d)" << endl;
}
Date& operator=(const Date& d)
{
cout << "Date& operator=(const Date& d)" << endl;
return *this;
}
~Date()
{
cout << "~Date()" << endl;
}
};
//void F1(Date& d)//情况一
void F1(Date d)//情况二
{}
int main()
{
Date d;
F1(d);
system("pause");
return 0;
}
问以上代码调用构造函数,拷贝构造函数,赋值操作符重载,析构函数的次数
结果如下:
第二题:
#include<iostream>
using namespace std;
class Date
{
public:
Date()
{
cout << "Date()" << endl;
}
Date(const Date& d)
{
cout << "Date(const Date& d)" << endl;
}
Date& operator=(const Date& d)
{
cout << "Date& operator=(const Date& d)" << endl;
return *this;
}
~Date()
{
cout << "~Date()" << endl;
}
};
//Date F2()
//{
// Date ret;
// return ret;
//}
Date F2()
{
Date ret;
return ret;
}
int main()
{
F2();
system("pause");
return 0;
}结果如下:
第三题:
第四题:
#include<iostream>
using namespace std;
class Date
{
public:
Date()
{
cout << "Date()" << endl;
}
Date(const Date& d)
{
cout << "Date(const Date& d)" << endl;
}
Date& operator=(const Date& d)
{
cout << "Date& operator=(const Date& d)" << endl;
return *this;
}
~Date()
{
cout << "~Date()" << endl;
}
};
void F1(Date d)
{
}
int main()
{
F1(Date());
system("pause");
return 0;
}结果:
第五题:压轴题
这就是面试中爱考的构造函数,拷贝构造函数优化问题。
第一题:
#include<iostream>
using namespace std;
class Date
{
public:
Date()
{
cout << "Date()" << endl;
}
Date(const Date& d)
{
cout << "Date(const Date& d)" << endl;
}
Date& operator=(const Date& d)
{
cout << "Date& operator=(const Date& d)" << endl;
return *this;
}
~Date()
{
cout << "~Date()" << endl;
}
};
//void F1(Date& d)//情况一
void F1(Date d)//情况二
{}
int main()
{
Date d;
F1(d);
system("pause");
return 0;
}
问以上代码调用构造函数,拷贝构造函数,赋值操作符重载,析构函数的次数
结果如下:
第二题:
#include<iostream>
using namespace std;
class Date
{
public:
Date()
{
cout << "Date()" << endl;
}
Date(const Date& d)
{
cout << "Date(const Date& d)" << endl;
}
Date& operator=(const Date& d)
{
cout << "Date& operator=(const Date& d)" << endl;
return *this;
}
~Date()
{
cout << "~Date()" << endl;
}
};
//Date F2()
//{
// Date ret;
// return ret;
//}
Date F2()
{
Date ret;
return ret;
}
int main()
{
F2();
system("pause");
return 0;
}结果如下:
第三题:
#include<iostream> using namespace std; class Date { public: Date() { cout << "Date()" << endl; } Date(const Date& d) { cout << "Date(const Date& d)" << endl; } Date& operator=(const Date& d) { cout << "Date& operator=(const Date& d)" << endl; return *this; } ~Date() { cout << "~Date()" << endl; } }; Date F2() { Date ret; return ret; } int main() { Date d1 = F2();//相当于Date d1(F2()) system("pause"); return 0; }结果:
第四题:
#include<iostream>
using namespace std;
class Date
{
public:
Date()
{
cout << "Date()" << endl;
}
Date(const Date& d)
{
cout << "Date(const Date& d)" << endl;
}
Date& operator=(const Date& d)
{
cout << "Date& operator=(const Date& d)" << endl;
return *this;
}
~Date()
{
cout << "~Date()" << endl;
}
};
void F1(Date d)
{
}
int main()
{
F1(Date());
system("pause");
return 0;
}结果:
第五题:压轴题
这就是面试中爱考的构造函数,拷贝构造函数优化问题。
相关文章推荐
- C++中的构造函数与拷贝构造的优化处理
- 4000 拷贝构造的优化问题
- 拷贝构造,深度拷贝,关于delete和default相关的操作,explicit,类赋初值,构造函数和析构函数,成员函数和内联函数,关于内存存储,默认参数,静态函数和普通函数,const函数,友元
- sql的优化相关问题
- 复数类的相关运算(判断大小及四则运算)->(构造,析构,拷贝复制,运算符重载)
- 讨论构造拷贝构造的N中调用情况的问题
- 深入探究JavaScript中for循环的效率问题及相关优化
- 字符串类的构造函数,拷贝构造,赋值函数的实现
- android 内存优化以及性能优化相关问题
- sql的优化相关问题
- js高级——构造函数属性相关的问题
- 析构函数、复制构造函数,operator=以及深拷贝浅拷贝问题
- C++之构造函数,拷贝构造,析构函数再剖析
- 讨论构造拷贝构造的N中调用情况的问题
- C++ Primer笔记9_构造函数_拷贝构造(深拷贝与浅拷贝)
- boolan第二周笔记:构造函数、拷贝构造、赋值构造、析构函数
- 记5.28大促压测的性能优化(线程池相关问题)
- android 内存优化以及性能优化相关问题
- 深度解析-->c++中构造函数,拷贝构造,赋值运算符重载,析构函数的调用情况
- EF(Entity Framework)发生错误”正在创建模型,此时不可使用上下文“的解决办法。 正在创建模型,此时不可使用上下文。如果在 OnModelCreating 方法内使用上下文或如果多个线程同时访问同一上下文实例,可能引发此异常。请注意不保证 DbContext 的实例成员和相关类是线程安全的。 临时解决了这个问题,在Context的构造函数中,禁用了自动初始化: