質問

I'm writing an application that involves a MySQL database, and I want to have all database-related operations in one (or more, in a future) specific files. I need to export a database connection ID to the other modules so they can use the services of the DB module.

Ideally, this would be. (in db.h)

typedef ????? dbconn_t

in other files

dbconn_t = db_newconnection(...);

The MySQL type for db connections is called MYSQL, so I was inclined to write

typedef MYSQL *dbconn;

But to do this, I would need to include mysql.h in my own db.h, exporting tons of functions and other types to the rest of the project.

Some solutions come to mind:

  • Export an int type and keep a table for mapping ints to MYSQL pointers

  • Export a void pointer and type cast in every call in database handling code

Both seem pretty bad. Any ideas to help this?

役に立ちましたか?

解決

You could encapsulate your data ID into a struct type.

For example, in your private headers you could have something like

struct dbconn {
     int id;
};

While in the header you export it as

struct dbconn;
typedef struct dbconn *dbconn_t;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top