Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Why returning temporary object
- by reference, like:
- Object & ref()
- {
- Object a(); return a;
- }
- isn't good idea.
- */
- #include <iostream>
- class simply
- {
- int * a;
- public:
- int getA()
- {
- return *a;
- }
- simply(const simply & copy)
- {
- delete a;
- a = new int;
- *a = *copy.a;
- }
- simply(int value)
- {
- a = new int;
- *a = value;
- }
- ~simply()
- {
- delete a;
- }
- };
- simply & a()
- {
- simply test(5);
- return test;
- }
- int main()
- {
- simply c(a());
- std::cout << c.getA();
- // Do we really will get 5? :)
- }
Advertisement
Add Comment
Please, Sign In to add comment