jeudi 20 juillet 2017

C++ smart pointers confusion

I understand that in the C++ realm it is advocated to use smart pointers. I have a simple program as below.

/* main.cpp */
#include <iostream>
#include <memory>
using namespace std;

/* SQLite */
#include "sqlite3.h"

int main(int argc, char** argv)
{
    // unique_ptr<sqlite3> db = nullptr; // Got error with this
    shared_ptr<sqlite3> db = nullptr;

    cout << "Database" << endl;
    return 0;
}

When I compile with unique_ptr line got an error message:

error C2027: use of undefined type 'sqlite3'
 error C2338: can't delete an incomplete type

When I compile with shared_ptr line it is successful. From several questions and answers my understanding is that unique_ptr should be preferred as I do not intended to have objects sharing resources. What is the best solution in this case? Use shared_ptr or go back to the old approach of bare pointers (new/delete)?

Aucun commentaire:

Enregistrer un commentaire