Access Object with shared_ptr

#include <iostream>
#include <memory>
using namespace std;

class Logger {
public:
    void log() { cout << "Logging data\n"; }
};

int main() {
    // your code here: declare a shared_ptr<Logger> and another shared_ptr that shares the same object
    shared_ptr<Logger> s1 = make_shared<Logger>();

    // Tạo shared_ptr khác chia sẻ cùng đối tượng
    shared_ptr<Logger> s2 = s1;

    // Gọi log() qua cả hai shared_ptr
    s1->log();
    s2->log();
    return 0;
}
Upvote
Downvote
Loading...

Input

Expected Output

Logging data Logging data