jeudi 29 novembre 2018

Return Local Object in C++ [duplicate]

This question already has an answer here:

Never calls move constructor. Why ? An error occurs when I delete the move constructor. Output:

A constructor

0x7fff8ed60ebf

Hello World!

0x7fff8ed60ebf

~A

#include <iostream>
#include <string>
class A{
public:
  A(){std::cerr<<"A constructor"<<std::endl;}
  A(A&& o){std::cerr<<"A move constructor"<<std::endl;}
  //A(A&& o) = delete;
  A(const A &p2){std::cerr<<"A copy constructor"<<std::endl;};
  ~A(){std::cerr<<"~A"<<std::endl;}
  A& operator=(const A& other)=delete;
  A& operator=(A&& other)=delete;

};

A foo(){

  A temp;
  std::cerr<<&temp<<std::endl;
  return temp;
}
int main() {
  A temp = foo();
  std::cerr << "Hello World!\n" << &temp << std::endl;
}

Aucun commentaire:

Enregistrer un commentaire