#include <iostream>
class Foo
{
public:
Foo()
:ptr(new int(0))
{
std::cout << ptr << "()" << std::endl;
}
Foo(int val)
:ptr(new int(val))
{
std::cout << ptr << "(" << val << ")" << std::endl;
}
Foo(const Foo& other)
:ptr(new int(other))
{
std::cout << ptr << "(&" << other.ptr << ")" << std::endl;
}
Foo(Foo&& other)
:ptr(new int (0))
{
std::cout << ptr << "(&&" << other.ptr << ")" << std::endl;
swap(other);
}
~Foo()
{
std::cout << "~" << ptr << std::endl;
delete ptr;
}
Foo& operator=(Foo other)
{
std::cout << ptr << "=" << other.ptr << std::endl;
return swap(other);
}
operator int() const
{
return *ptr;
}
Foo& swap(Foo& other)
{
std::cout << ptr << "~" << other.ptr << std::endl;
std::swap(ptr, other.ptr);
}
private:
int *ptr;
};
int main()
{
Foo a(1);
Foo b(std::move(a));
std::cout << a << " " << b << std::endl;
Foo c(2);
c = std::move(b);
std::cout << c << " " << b << std::endl;
Foo d(3);
d = b;
std::cout << d << " " << b << std::endl;
}
0x25216d0(1)0x2521720(&&0x25216d0)0x2521720~0x25216d00 10x2523b60(2)0x2567d20(&&0x25216d0)0x2567d20~0x25216d00x2523b60=0x25216d00x2523b60~0x25216d0~0x2523b601 00x2523b60(3)0x2568c20(&0x2567d20)0x2523b60=0x2568c200x2523b60~0x2568c20~0x2523b600 0~0x2568c20~0x25216d0~0x2567d20~0x2521720
Foo& operator~(Foo& other)
{
x ~ other.x; //int
y ~ other.y; //int
z ~ other.z; //int
ptr ~ other.ptr; // ptr to int
bar ~ other.bar; // class Bar
}
Foo& operator=(const Foo& other)
{
x = other.x;
y = other.y;
z = other.z;
ptr = other.ptr;
bar = other.bar;
return *this;
}
Foo& operator=(Foo other)
{
return *this ~ other;
}
Foo& operator=(const Foo& other)
{
return *this ~ Foo(other);
}
Foo& operator=(Foo&& other)
{
return *this ~ other;
}
Foo(Foo&& other)
{
Foo::Foo(); // default constructor
*this ~ other;
}
class Foo
{
public:
Foo()
:ptr(new int(0))
{
std::cout << ptr << "()" << std::endl;
}
Foo(int val)
:ptr(new int(val))
{
std::cout << ptr << "(" << val << ")" << std::endl;
}
Foo(const Foo& other)
:ptr(new int(other))
{
std::cout << ptr << "(&" << other.ptr << ")" << std::endl;
}
~Foo()
{
std::cout << "~" << ptr << std::endl;
delete ptr;
}
operator int() const
{
return *ptr;
}
private:
int *ptr;
};
int main()
{
Foo a(1);
Foo b(std::move(a));
std::cout << a << " " << b << std::endl;
Foo c(2);
c ~ b;
std::cout << c << " " << b << std::endl;
Foo d(3);
d = b;
std::cout << d << " " << b << std::endl;
}
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--
---
You received this message because you are subscribed to a topic in the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this topic, visit https://groups.google.com/a/isocpp.org/d/topic/std-proposals/40i99Eqtt3I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to std-proposal...@isocpp.org.