|
#include <iostream> |
|
#include <memory> |
|
#include <string> |
|
|
|
// This is just an example object with a text field. |
|
class Content { |
|
public: |
|
std::string txt; |
|
Content(std::string txt) : txt(txt){}; |
|
}; |
|
|
|
// This function will print the number of references in the shared_pointer and the text field of the Content object. |
|
// Note how we pass the the shared_ptr of Content as a renference since we are just reading its fields. This does not |
|
// creates a copy nor increase the counter of the shared_ptr. |
|
void println(const std::shared_ptr<Content>& c) |
|
{ |
|
std::cout << "count = " << c.use_count() << " content = " << c->txt << std::endl; |
|
} |
|
|
|
// This function will change the text field of the Content object pointed by the shared_ptr. As before, this shared |
|
// pointer is passed as a reference and we manipulate directly the text field. Note how to assign a value to the |
|
// "txt" field the arrow '->' operator is used, just like a normal pointer. |
|
void manipulate(const std::shared_ptr<Content>& c, std::string new_txt) |
|
{ |
|
std::cout << "txt before: " << c->txt << std::endl; |
|
c->txt = new_txt; |
|
std::cout << "txt after: " << c->txt << std::endl; |
|
} |
|
|
|
// This function allocates a new Content object with the given value of the txt field and wraps it using the |
|
// constructor of std::shared_ptr. Then it returns this shared pointer. |
|
std::shared_ptr<Content> getContent(std::string txt) |
|
{ |
|
std::shared_ptr<Content> c(new Content(txt)); |
|
std::cout << "start getContent()" << std::endl; |
|
println(c); |
|
auto d = c; |
|
std::cout << "after assignment" << std::endl; |
|
println(d); |
|
return c; |
|
} |
|
|
|
// Same as the previous function, but this time we are using the std::make_shared<> utility function. Thi function |
|
// passes the arguments directly to the constructor of the shared object. We don't need to construct an object |
|
// first using the "new" keyword and it also guarantees that the shared pointer and object are contiguous in the |
|
// memory. |
|
std::shared_ptr<Content> makeContent(std::string txt) |
|
{ |
|
std::shared_ptr<Content> a = std::make_shared<Content>(txt); |
|
std::cout << "make getContent()" << std::endl; |
|
println(a); |
|
auto b = a; |
|
std::cout << "after assignment" << std::endl; |
|
println(b); |
|
return a; |
|
} |
|
|
|
int main() |
|
{ |
|
std::cout << "Using make_shared" << std::endl; |
|
std::shared_ptr<Content> a = std::make_shared<Content>("a"); |
|
std::cout << "count a = " << a.use_count() << std::endl; |
|
println(a); |
|
std::cout << "count a = " << a.use_count() << std::endl; |
|
|
|
std::cout << "Manipulating the object" << std::endl; |
|
println(a); |
|
manipulate(a, "A"); |
|
println(a); |
|
|
|
std::cout << "Using getContent() function" << std::endl; |
|
std::shared_ptr<Content> x = getContent("x"); |
|
std::shared_ptr<Content> y = getContent("y"); |
|
println(x); |
|
println(y); |
|
auto z = y; |
|
println(z); |
|
|
|
std::cout << "Using make_shared() function" << std::endl; |
|
std::shared_ptr<Content> w = std::make_shared<Content>("w"); |
|
println(w); |
|
auto w1 = w; |
|
println(w1); |
|
auto w2 = w; |
|
println(w2); |
|
|
|
std::cout << "Using makeContent() function" << std::endl; |
|
auto h = makeContent("make"); |
|
println(h); |
|
auto m = h; |
|
println(m); |
|
} |