I hope to prevent the users from creating new instance through the constructor, so I mark the constructor as a private method.
What's more, I need to provide a method to return an type which is used to automatically manage the life of the instance. I don't hope the user to use the instance of Foo class directly, I hope they always use std::shared_ptr<Foo> instead.
#include <iostream>
#include <memory>
 
class Foo : public std::enable_shared_from_this<Foo> {
private:     //the user should not construct an instance through the constructor below.                    
    Foo(int num):num_(num) { std::cout << "Foo::Foo\n"; }
public:
    Foo(const Foo&) = delete;
    Foo(Foo&&) = default;
    Foo& operator=(const Foo&) = delete;
    Foo& operator=(Foo&&) = default;
public:
    ~Foo() { std::cout << "Foo::~Foo\n"; } 
    int DoSth(){std::cout << "hello world" << std::endl; return 0;}
    std::shared_ptr<Foo> getPtr() { return shared_from_this();}
    static std::shared_ptr<Foo> Create() {
        Foo* foo = new Foo(5);
        return std::shared_ptr<Foo>(foo);
    }
private:
    int num_;
};
int main()
{
    auto sp = Foo::Create();
    sp->DoSth();
    Foo& foo = *sp.get();
    auto sp1 = foo.getPtr();
    Foo foo1(std::move(foo));
    std::cout << sp.use_count() << std::endl;
}
I am not sure whether or not I should mark the move constructor & move assignment operator as deleted.
Aucun commentaire:
Enregistrer un commentaire