我试图创建临时对象的方式有错误吗
Is there any error in the way I am trying to create temporrary object
下面是我的代码:我不明白为什么不调用move构造函数。
Mystring.h:
#ifndef _MYSTRING_H
#define _MYSTRING_H
#include<iostream>
#include<cstring>
#endif
class Mystring{
private:
char * str;
public:
Mystring(); //No arg constructor
Mystring(char *str); //Overloaded constructor
Mystring(const Mystring &source); //Copy constructor
Mystring(Mystring &&source); //Move constructor
//operator overloading
bool operator==(const Mystring &rhs);
bool operator!=(const Mystring &rhs);
bool operator<(const Mystring &rhs);
bool operator>(const Mystring &rhs);
Mystring &operator+(const Mystring &rhs);
};
Mystring.cpp:
#include"mystring.h"
Mystring::Mystring():str{nullptr}{
std::cout<<"No args constructor"<<std::endl;
}
Mystring::Mystring(char *str){
this->str = nullptr;
this->str = new char[strlen(str)+1];
strcpy(this->str,str);
std::cout<<"Overloaded constructor called"<<std::endl;
}
Mystring::Mystring(const Mystring &source){
delete[] this->str;
this->str = nullptr;
this->str = new char[strlen(source.str) + 1];
strcpy(this->str,source.str);
std::cout<<"copy constructor called"<<std::endl;
}
Mystring::Mystring(Mystring &&source){
this->str = source.str;
source.str = nullptr;
std::cout<<"Move constructor called"<<std::endl;
}
bool Mystring::operator==(const Mystring &rhs){
std::cout<<"operator == called"<<std::endl;
return strcmp(this->str,rhs.str);
}
bool Mystring::operator!=(const Mystring &rhs){
std::cout<<"operator != called"<<std::endl;
return !(operator==(rhs));
}
bool Mystring::operator <(const Mystring &rhs){
std::cout<<"operator < called"<<std::endl;
if(strcmp(this->str,rhs.str) < 0){
return 0;
}
else
return 1;
}
bool Mystring::operator >(const Mystring &rhs){
std::cout<<"operator > called"<<std::endl;
return(!(operator<(rhs)));
}
Mystring & Mystring::operator+(const Mystring &rhs){
std::cout<<"operator+ called"<<std::endl;
char *temp = new char[strlen(this->str) + strlen(rhs.str)+ 1];
strcat(temp,this->str);
strcat(temp,rhs.str);
}
main.cpp:
#include"Mystring.h"
int main(){
Mystring A {"cat"}; //Overloaded constructor
Mystring B {"dog"}; //Overloaded constructor
Mystring C {A}; //Copy constructor
Mystring D {Mystring{"Hello"}}; //Overloaded and then move constructor
return 0;
}
Myquestions:
语句Mystring D {Mystring{"Hello"}};
从不调用move构造函数。我不明白为什么Mystring {"Hello"}
不生成任何临时对象,然后应该调用move构造函数。
语句
Mystring D {Mystring{"Hello"}};
从不调用移动构造函数
因为复制省略;在这种情况下,这是由C++17保证的。
当一个对象从相同类型的prvalue初始化时(忽略cv限定(,它将直接从初始化器初始化。不会创建临时文件,也不会进行复制或移动。这有时被称为"保证拷贝省略",尽管与拷贝省略不同,它是强制性的,不是优化。
这意味着给定Mystring D {Mystring{"Hello"}};
,D
直接从"Hello"
初始化。无临时操作,无复制/移动操作。
相关文章:
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 我的代码中有错误,未声明的标识符
- C++通过别名指针以静默方式将错误的类型分配给数组元素
- C++ LeetCode #377 的 DP 解决方案中,此代码是否有错误?
- 试图建立银行管理系统,但有错误
- 我试图创建临时对象的方式有错误吗
- 我的随机生成器是否不工作,或者我决定人/骨架是否击中对手的方式是否有错误
- 我在OpenCV中有错误的"approxPolyDP(ROI_Vertices, ROI_Poly, 1.0, true)"
- 我在这个 C++ 代码上有错误吗?
- 是视觉C++优化器错误还是我的代码中有错误?
- 为什么一个简单的C++程序会有错误的结果?是 #define 引起的吗?
- 对此有错误
- C :数据文件有错误:预期的无限制ID
- 一个带有参数的程序,为什么此程序有错误的答案
- 仅使用boost.datetime在Windows上有错误
- 使用向量循环时,是否有错误
- Microsoft Visual Studio发现我所有的CUDA设备端函数都有错误
- 对Getline的工作方式有更深入的深度
- 这两种方式有什么区别吗?
- 继承默认构造函数在 gcc 中失败并在 clang 中工作,哪个有错误?