I'm using the mysqlx-api/xdevapi.h (mysql connector 8) for my application. My code is object oriented, and I want to initialize the connection to the database in the class constructor. It that possible? If not, is there any disadvantages of initializing class members in the header file, outside of the constructor?
Below is my working code.
MysqlClass.hpp
#include <iostream>
#include <string>
#include <xdevapi.h>
class MysqlClass {
private:
mysqlx::abi2::r0::Session session = mysqlx::abi2::r0::Session(mysqlx::abi2::r0::SessionOption::HOST, "localhost",
mysqlx::abi2::r0::SessionOption::PORT, 33060,
mysqlx::abi2::r0::SessionOption::USER, "root",
mysqlx::abi2::r0::SessionOption::PWD, "abc");
mysqlx::abi2::r0::Schema db= session.getSchema("MySchema");
mysqlx::abi2::r0::Table table_obs =db.getTable("MyTable");
public:
MysqlClass(void);
};
MysqlClass.cpp
#include <iostream>
#include <string>
#include <xdevapi.h>
#include "MysqlClass.hpp"
MysqlClass::MysqlClass(void) {
/* I would like to establish connection to the database here. */
//this->session = mysqlx::abi2::r0::Session(mysqlx::abi2::r0::SessionOption::HOST, "localhost",
// mysqlx::abi2::r0::SessionOption::PORT, 33060,
// mysqlx::abi2::r0::SessionOption::USER, "root",
// mysqlx::abi2::r0::SessionOption::PWD, "abc");
std::cout << "Hello" << std::endl;
}
test.cpp
#include <iostream>
#include <cstdint>
#include <cstdio>
#include <string>
#include <xdevapi.h>
#include "MysqlClass.hpp"
int main () {
try
{
MysqlClass conn = MysqlClass();
}catch(const mysqlx::Error &err)
{
std::cout << "Error connecting to the database." << err << std::endl;
}
}
This is the error I get when I move the initialization to the constructor:
../MysqlClass.cpp: In constructor ‘MysqlClass::MysqlClass()’:
/home/MySQL-cpp-test/MysqlClass.cpp:13:75: error: use of deleted function ‘mysqlx::abi2::r0::Session& mysqlx::abi2::r0::Session::operator=(const mysqlx::abi2::r0::Session&)’
13 | mysqlx::abi2::r0::SessionOption::PWD, "abc");
| ^
In file included from ../MysqlClass.cpp:4:
/usr/include/mysql-cppconn-8/mysqlx/xdevapi.h:1672:7: note: ‘mysqlx::abi2::r0::Session& mysqlx::abi2::r0::Session::operator=(const mysqlx::abi2::r0::Session&)’ is implicitly declared as deleted because ‘mysqlx::abi2::r0::Session’ declares a move constructor or move assignment operator
1672 | class Session
| ^~~~~~~
The code then looks like this:
class MysqlClass {
private:
mysqlx::abi2::r0::Session session;
public:
MysqlClass(void);
};
MysqlClass::MysqlClass(void) {
this->session = mysqlx::abi2::r0::Session(mysqlx::abi2::r0::SessionOption::HOST, "localhost",
mysqlx::abi2::r0::SessionOption::PORT, 33060,
mysqlx::abi2::r0::SessionOption::USER, "root",
mysqlx::abi2::r0::SessionOption::PWD, "root");
std::cout << "Hello" << std::endl;
}
int main () {
try
{
MysqlClass conn = MysqlClass();
}catch(const mysqlx::Error &err)
{
std::cout << "Error connecting to the database." << err << std::endl;
}
}
This is standard C++, use an initialiser list
MysqlClass::MysqlClass()
: session(mysqlx::abi2::r0::SessionOption::HOST, "localhost",
mysqlx::abi2::r0::SessionOption::PORT, 33060,
mysqlx::abi2::r0::SessionOption::USER, "root",
mysqlx::abi2::r0::SessionOption::PWD, "abc"))
{
std::cout << "Hello" << std::endl;
}
Generally speaking, class members should always be initialised in an initialiser list, instead of assigned in the body of the constructor.