Following is my code: I dont understand why the move constructor does not get invoked.
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;
}
Mydoubts:
The statement Mystring D {Mystring{"Hello"}};
never invokes move constructor. I don't understand why Mystring {"Hello"}
doesn't generate any temporary object following which move constructor should be called.