Created
October 13, 2013 00:52
-
-
Save BBBSnowball/6956715 to your computer and use it in GitHub Desktop.
Test program to show the difference of reference, pointer and copy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <memory> | |
class Foo { | |
std::string name; | |
public: | |
Foo(std::string name) : name(name) { | |
std::cout << "creating " << name << std::endl; | |
} | |
~Foo() { | |
std::cout << "deleting " << name << std::endl; | |
} | |
Foo(const Foo& foo) { | |
std::cout << "copy " << foo.name << std::endl; | |
this->name = foo.name; | |
} | |
void test() { | |
std::cout << "test: " << name << std::endl; | |
} | |
}; | |
int main(int argc, char** argv) { | |
Foo blub("abc"); | |
std::shared_ptr<Foo> sblub = new Foo("shared_ptr abc"); | |
Foo pblub("problem"); | |
{ | |
Foo& a = blub; | |
std::shared_ptr<Foo> b = sblub; | |
Foo c = pblub; | |
a.test(); | |
b->test(); | |
c.test(); | |
} | |
//blub | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment