mercredi 30 octobre 2019

If my class mnages a resource correctly then what is the point in having smart poointers?

I am new to smart pointers, I liked using them for the safety and power of sharing object...

I have one question: If my class manages a resource in its constructors and destructors applying some rules of thumb like the Big 5 and the Big 3... Should I still use smart pointers? or my class is an alternative to them. Because as I've read in C++ primer 5 edition that smart pointers came to solve problems raw pointers were facing like memory-leaks, double-deleting pointers and accessing a dangling pointer... My class can avoid those problems:

class BallGame {
    public:
        using Resource = int;

        BallGame(int);
        BallGame(const BallGame&);
        BallGame(BallGame&&);
        BallGame& operator=(const BallGame&);
        BallGame& operator=(BallGame&&);
        ~BallGame();
    private:
        Resource m_res;
};
  • Consider that the member of my class are doing the right job so can I avoid smart pointer?

  • I want to know some scenarios when I should use smart pointers rather than managing resources in my class.

  • Are they really for "dumb classes" (classes that define constructors but not well-behaved destructors) like in C++ primer book.

Thank you.

Aucun commentaire:

Enregistrer un commentaire