mysql connection object in other classes

MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "server";
char *user = "user";
char *password = "password"; // got tot keep my data secret
char *database = "cpp_test";
conn = mysql_init(NULL);

// connect to database
if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return -1;
}



ok, I've managed to successfully compile this code under linux, but I have a question
I have got another class with a member variable of type MYSQL *conn and I'd like to use the same connection

can I simply do this class2.conn = conn?






Yes.

But you don't appear to be respecting any kind of model, so we can't comment on the effect of doing so. The issues are: creation, copy, assingment and deletion.

Can you guaranee that these are done correctly without having dangling connections or connections closed more than once?
I've tested it with valgrind, and it's bad. Memory leaks, and it gets bigger each time the function is called. So I should probably use pointers, or pass value by reference?
Any suggestions? or examples pls

Topic archived. No new replies allowed.