Witam!
Jak napisać w C++ (nie w c++11) konstruktor przenoszący?
Jestem programistą C, kiedyś pisałem coś w C++, ale swoją wiedzę oceniam jako dno, chcę powtórzyć i zrozumieć wszystkie podstawowe mechanizmy.
Doczytałem, że każda klasa ma domyślne konstruktory i operatory, które nie są jawne. Chciałbym nadpisać je wszystkie, by zrozumieć jak działają konstruktory/operatory kopiowania i przenoszenia.
#include <iostream>
class Foo{
public:
Foo(int foo)
{
// std::cout << __FUNCTON__ << std::endl; // wyswietla nazwe klasy :(
std::cout << "Foo(int foo)" << std::endl;
}
Foo(Foo& other)
{
std::cout << "Foo(Foo& other)" << std::endl;
}
Foo(const Foo& other)
{
std::cout << "Foo(const Foo& other)" << std::endl;
}
Foo& operator=(Foo& other)
{
std::cout << "Foo& operator=(Foo& other)" << std::endl;
return *this;
}
Foo& operator=(const Foo& other)
{
std::cout << "Foo& operator=(const Foo &other)" << std::endl;
return *this;
}
void print() const
{
std::cout << "void test() const" << std::endl;
}
private:
};
Foo test(){
Foo foo(2);
return foo;
}
int main(){
Foo foo = test();
foo.print();
return 0;
}
Wykonując return foo, spodziewałem się że wykona się chociaż konstruktor/operator kopiowania, lecz wynik jest taki:
Foo(int foo)
void test() const