简体   繁体   中英

SQLite char* conversion in C++

I've recently been relearning C++ and creating an application in hand with what I'm learning and even branching out to figure out concepts that aren't necessarily normal. As dangerous as I see this being, I am still diving head first.

That being said the application I'm currently working on requires storing some info in a local database which I will then be merging with a server database at some point in the future to allow for more in-depth queries and a better UI. While diving into learning SQLite3 integration with C++ I've found that a lot of the integration is specifically "C/C++" with, what appears to be, a stronger foot in C than C++. With this realization I've come across one very specific tutorial that leaned on C++ over C minus the specific issue I'm encountering.

https://www.dreamincode.net/forums/topic/122300-sqlite-in-c/

I actually rather like the concise nature of the Database.cpp that the author of the tutorial created and I want to utilize it. The problem is C++ likes to throw the conversion warnings that apparently work due to the use of C, but are deprecated in C++.

ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

Technically this can be bypassed by casting the string to (char*). While I understand this it seems I may be missing some information as well. Could this be bypassed by changing the parameters of the Database function to "string*" then converting it to "char*" in the function or should I not care about the implicit conversion and just ignore it. I really don't want to cross C and C++ in my application so I would prefer to paid heed to the warning. Figured I'd ask for advice to at least get some clarification though.

If it seems obvious from my inquiry that I am lacking in some very specific section of my C++ knowledge please feel free to let me know. I am nothing if not diligent when it comes to making sure I can fill all the gaps in my knowledge on any given topic.

You should tell the author of that tutorial to make his code const correct if you want to make use of his class.

Well, actually, I suggest you edit it yourself to make it conform. This won't take you long once you understand the principles involved (which are not hard to get your head around), and doing this will help you write your own code in the right way.

So, just by way of example, change this:

class Database
{
public:
    Database(char* filename);
    ...

To this (note the added const ):

class Database
{
public:
    Database(const char* filename);
    ...

There are, of course, a bunch of other related changes you need to make but you just have to see it through, and the compiler itself will guide you when you get it wrong. SQLlite itself is already const correct (because those guys are professionals), so there is light at the end of the tunnel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM