#include <iostream>
class Box
{
public:
Box()
{
std::cout << "constructor called" << std::endl;
}
Box(const Box& box){
std::cout << "Copy constructor called" << std::endl;
}
Box(Box&& box){
std::cout << "Move constructor called" << std::endl;
}
void run(){ std::cout << "Run" << std::endl;}
};
int main()
{
Box a(Box());
a.run();
return 0;
}
In the above code I was expecting either Copy Constuctor or Move Constructor to be called on passing anonymous object Box() as argument. But none of them where called. Reason probably might be copy elision. But even constructor is not called for anonymous object A(). For calling run() function compiler gave following error.
a.cpp: In function ‘int main()’:
a.cpp:28:7: error: request for member ‘run’ in ‘a’, which is of non-class type ‘Box(Box (*)())’
a.run();
So when we type Box a(Box()) what is happening? What is being created?
Aucun commentaire:
Enregistrer un commentaire