TSharedPtr is a smart pointer class template in C++, which provides shared ownership of a heap-allocated object. Multiple TSharedPtr instances can share ownership of a single object while retaining the ability to determine the last one which referenced the object and free its resources accordingly.
Example 1:
```c++ #include #include
int main() { std::shared_ptr mySharedPtr(new int); *mySharedPtr = 42; // set the value
*mySharedPtr = 13; // change the value
std::cout << *mySharedPtr << std::endl; // prints 13
return 0; }
Example 2:
c++
#include
#include
class MyClass {
public:
MyClass() {
std::cout << "Constructor called" << std::endl;
}
~MyClass() {
std::cout << "Destructor called" << std::endl;
}
void sayHello() {
std::cout << "Hello World!" << std::endl;
}
};
int main() {
std::shared_ptr mySharedPtr1(new MyClass);
std::shared_ptr mySharedPtr2;
mySharedPtr2 = mySharedPtr1;
std::cout << "Before reset() was called" << std::endl;
mySharedPtr1->sayHello();
mySharedPtr2->sayHello();
mySharedPtr1.reset();
std::cout << "After reset() was called" << std::endl;
mySharedPtr2->sayHello(); // This still works
// mySharedPtr1->sayHello(); // This would crash the program, since the object has been deleted
return 0;
}
```
These examples use the C++ Standard Library's shared pointer functionality, which is part of the package.
C++ (Cpp) TSharedPtr - 1 examples found. These are the top rated real world C++ (Cpp) examples of TSharedPtr extracted from open source projects. You can rate examples to help us improve the quality of examples.