admin管理员组文章数量:1025465
Below is my C++ code. I was expecting my move ctor to be called here in my main
:
Mystring larry2 = "some larry";
main.cpp:
int main()
{
//Mystring no_name;
Mystring larry ("LARRY");
Mystring larry1 {larry}; // deep copy ctor is called
Mystring larry2 = "some larry"; // move ctor should be called but still calling parameterized ctor
}
Mystring.cpp:
Mystring::Mystring()
:str{nullptr} {
cout <<"default ctor called" <<endl;
str = new char[1];
*str = '\0';
}
Mystring::Mystring(const char *s)
:str{nullptr} {
cout << "param ctor called" << endl;
if(s == nullptr) {
// then do same as default ctor;
str = new char[1];
*str = '\0';
}else {
str = new char[std::strlen(s) + 1];
std::strcpy(str, s);
}
}
Mystring::~Mystring() {
cout << "dtor called: " << this->str << endl;
delete []str;
}
//deep copy ctor implementation
Mystring::Mystring(const Mystring &source)
:str{nullptr} {
cout << "deep copy ctor called" <<endl;
str = new char[std::strlen(source.str) + 1];
std::strcpy(str, source.str);
}
//move ctor implementation
Mystring::Mystring(Mystring &&source) noexcept
:str(source.str) {
cout << "move ctor called" << endl;
source.str = nullptr;
}
void Mystring::display() const {
cout << this->str << endl;
}
Mystring.h:
class Mystring {
private:
char *str;
public:
Mystring(); // default ctor
Mystring(const char *source); //parameter ctor
~Mystring();
Mystring(const Mystring &source); //copy ctor
Mystring(Mystring &&source) noexcept; //move ctor
void display() const;
};
I tried multiple sources.
Below is my C++ code. I was expecting my move ctor to be called here in my main
:
Mystring larry2 = "some larry";
main.cpp:
int main()
{
//Mystring no_name;
Mystring larry ("LARRY");
Mystring larry1 {larry}; // deep copy ctor is called
Mystring larry2 = "some larry"; // move ctor should be called but still calling parameterized ctor
}
Mystring.cpp:
Mystring::Mystring()
:str{nullptr} {
cout <<"default ctor called" <<endl;
str = new char[1];
*str = '\0';
}
Mystring::Mystring(const char *s)
:str{nullptr} {
cout << "param ctor called" << endl;
if(s == nullptr) {
// then do same as default ctor;
str = new char[1];
*str = '\0';
}else {
str = new char[std::strlen(s) + 1];
std::strcpy(str, s);
}
}
Mystring::~Mystring() {
cout << "dtor called: " << this->str << endl;
delete []str;
}
//deep copy ctor implementation
Mystring::Mystring(const Mystring &source)
:str{nullptr} {
cout << "deep copy ctor called" <<endl;
str = new char[std::strlen(source.str) + 1];
std::strcpy(str, source.str);
}
//move ctor implementation
Mystring::Mystring(Mystring &&source) noexcept
:str(source.str) {
cout << "move ctor called" << endl;
source.str = nullptr;
}
void Mystring::display() const {
cout << this->str << endl;
}
Mystring.h:
class Mystring {
private:
char *str;
public:
Mystring(); // default ctor
Mystring(const char *source); //parameter ctor
~Mystring();
Mystring(const Mystring &source); //copy ctor
Mystring(Mystring &&source) noexcept; //move ctor
void display() const;
};
I tried multiple sources.
Share Improve this question edited Nov 18, 2024 at 16:22 genpfault 52.2k12 gold badges91 silver badges151 bronze badges asked Nov 17, 2024 at 17:27 gaurav sgaurav s 595 bronze badges 1- 1 "move ctor should be called" -- copy/move constructors can be elided even when they are logically present – Gene Commented Nov 17, 2024 at 19:15
1 Answer
Reset to default 5I assume you expected a move ctor in this line:
Mystring larry2 = "some larry";
because you thought a temporary object is first constructer from "some larry"
, and then it is moved into larry2
.
But in fact this line is not performing an assignment, but rather an intialization of larry2
.
It is equivalent to:
Mystring larry2{"some larry"}; // or: larry2("some larry");
And therefore invokes a single call of the constructor that accepts a const char*
.
A side note:
It's better to avoid using namespace std;
- see here.
Below is my C++ code. I was expecting my move ctor to be called here in my main
:
Mystring larry2 = "some larry";
main.cpp:
int main()
{
//Mystring no_name;
Mystring larry ("LARRY");
Mystring larry1 {larry}; // deep copy ctor is called
Mystring larry2 = "some larry"; // move ctor should be called but still calling parameterized ctor
}
Mystring.cpp:
Mystring::Mystring()
:str{nullptr} {
cout <<"default ctor called" <<endl;
str = new char[1];
*str = '\0';
}
Mystring::Mystring(const char *s)
:str{nullptr} {
cout << "param ctor called" << endl;
if(s == nullptr) {
// then do same as default ctor;
str = new char[1];
*str = '\0';
}else {
str = new char[std::strlen(s) + 1];
std::strcpy(str, s);
}
}
Mystring::~Mystring() {
cout << "dtor called: " << this->str << endl;
delete []str;
}
//deep copy ctor implementation
Mystring::Mystring(const Mystring &source)
:str{nullptr} {
cout << "deep copy ctor called" <<endl;
str = new char[std::strlen(source.str) + 1];
std::strcpy(str, source.str);
}
//move ctor implementation
Mystring::Mystring(Mystring &&source) noexcept
:str(source.str) {
cout << "move ctor called" << endl;
source.str = nullptr;
}
void Mystring::display() const {
cout << this->str << endl;
}
Mystring.h:
class Mystring {
private:
char *str;
public:
Mystring(); // default ctor
Mystring(const char *source); //parameter ctor
~Mystring();
Mystring(const Mystring &source); //copy ctor
Mystring(Mystring &&source) noexcept; //move ctor
void display() const;
};
I tried multiple sources.
Below is my C++ code. I was expecting my move ctor to be called here in my main
:
Mystring larry2 = "some larry";
main.cpp:
int main()
{
//Mystring no_name;
Mystring larry ("LARRY");
Mystring larry1 {larry}; // deep copy ctor is called
Mystring larry2 = "some larry"; // move ctor should be called but still calling parameterized ctor
}
Mystring.cpp:
Mystring::Mystring()
:str{nullptr} {
cout <<"default ctor called" <<endl;
str = new char[1];
*str = '\0';
}
Mystring::Mystring(const char *s)
:str{nullptr} {
cout << "param ctor called" << endl;
if(s == nullptr) {
// then do same as default ctor;
str = new char[1];
*str = '\0';
}else {
str = new char[std::strlen(s) + 1];
std::strcpy(str, s);
}
}
Mystring::~Mystring() {
cout << "dtor called: " << this->str << endl;
delete []str;
}
//deep copy ctor implementation
Mystring::Mystring(const Mystring &source)
:str{nullptr} {
cout << "deep copy ctor called" <<endl;
str = new char[std::strlen(source.str) + 1];
std::strcpy(str, source.str);
}
//move ctor implementation
Mystring::Mystring(Mystring &&source) noexcept
:str(source.str) {
cout << "move ctor called" << endl;
source.str = nullptr;
}
void Mystring::display() const {
cout << this->str << endl;
}
Mystring.h:
class Mystring {
private:
char *str;
public:
Mystring(); // default ctor
Mystring(const char *source); //parameter ctor
~Mystring();
Mystring(const Mystring &source); //copy ctor
Mystring(Mystring &&source) noexcept; //move ctor
void display() const;
};
I tried multiple sources.
Share Improve this question edited Nov 18, 2024 at 16:22 genpfault 52.2k12 gold badges91 silver badges151 bronze badges asked Nov 17, 2024 at 17:27 gaurav sgaurav s 595 bronze badges 1- 1 "move ctor should be called" -- copy/move constructors can be elided even when they are logically present – Gene Commented Nov 17, 2024 at 19:15
1 Answer
Reset to default 5I assume you expected a move ctor in this line:
Mystring larry2 = "some larry";
because you thought a temporary object is first constructer from "some larry"
, and then it is moved into larry2
.
But in fact this line is not performing an assignment, but rather an intialization of larry2
.
It is equivalent to:
Mystring larry2{"some larry"}; // or: larry2("some larry");
And therefore invokes a single call of the constructor that accepts a const char*
.
A side note:
It's better to avoid using namespace std;
- see here.
本文标签: cWhy is the move constructor is not being calledStack Overflow
版权声明:本文标题:c++ - Why is the move constructor is not being called? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745629575a2160077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论