Skip to content

Instantly share code, notes, and snippets.

@BBBSnowball
Created October 13, 2013 00:52
Show Gist options
  • Save BBBSnowball/6956715 to your computer and use it in GitHub Desktop.
Save BBBSnowball/6956715 to your computer and use it in GitHub Desktop.
Test program to show the difference of reference, pointer and copy
#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