Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct A
- {
- A()
- {
- std::cout << "A::A()" << std::endl;
- }
- A(A const &other)
- {
- std::cout << "A::A(A const &other)" << std::endl;
- }
- ~A()
- {
- std::cout << "A::~A" << std::endl;
- }
- };
- struct Keeper
- {
- A getByValue()
- {
- return a_;
- }
- A &getByRef()
- {
- return a_;
- }
- A a_;
- };
- int main()
- {
- Keeper k;
- {
- A &local = k.getByRef();
- std::cout << "get by ref" << std::endl;
- }
- {
- A const &local = k.getByValue();
- std::cout << "get by value to ref" << std::endl;
- }
- //A &local = k.getByValue();
- //error: invalid initialization of non-const reference of type ‘A&’ from an rvalue of type ‘A’
- {
- A local = k.getByValue();
- std::cout << "get by value to value" << std::endl;
- }
- std::cout << "program end" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement