Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Destructor Called Multiple Times
    text
    copied!<p>I'm making a little wrapper class for sqlite. To get data to/from the database I have a class called SQLiteValue. When binding data for a query SQLiteValue instances get created on the stack and passed around a few functions. A skeleton outline of the class is below.</p> <pre><code>class SQLiteValue : public SQLiteObject { private: // stores a pointer to the data contained (could be of varying types) union { int* i; double* d; std::string* s; std::wstring* ws; BYTE* b; } pdata; int type; public: SQLiteValue(const char* val); SQLiteValue(const wchar_t* val); .. and so on for varying types virtual ~SQLiteValue(); }; </code></pre> <p>The object gets created by one of several overloaded constructors. The constructors instantiate a "member" of pdata based on their type. This is the important thing for this class. Now, the problem. I have the constructors overloaded so I get clean method calls and don't need to explicitly call SQLiteValue(xxx). As such I don't really want to use references for functions, so I define them like.</p> <pre><code>void BindValue(const char* name, SQLiteValue value) query-&gt;BindValue(":username", "user2"); // the "clean" method call </code></pre> <p>Declaring them like this causes a new object to be instantiated every time (or something similar?) I call a function and so the destructor frees memory allocated for pdata. This is bad.</p> <p>What I'd like to know is this. Is there a better way to achieve what I'm trying to do whilst retaining my clean method calls? At the moment I have private functions which operate by reference which solves the issue, but I don't really like this method. It would be easy for me to forget the reference and I'd end up tracking down this same issue again.</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload