c++

Assign C++在智能指针使用中的案例

小樊
82
2024-07-21 13:10:01
栏目: 编程语言

#include #include

class Test { public: Test() { std::cout << “Test constructor” << std::endl; } ~Test() { std::cout << “Test destructor” << std::endl; } void print() { std::cout << “Printing from Test class” << std::endl; } };

int main() { // Using unique_ptr std::unique_ptr uniquePtr(new Test); uniquePtr->print();

// Using shared_ptr
std::shared_ptr<Test> sharedPtr1(new Test);
std::shared_ptr<Test> sharedPtr2 = sharedPtr1;
sharedPtr1->print();
sharedPtr2->print();

return 0;

}

0
看了该问题的人还看了