Declaration of DbDatabaseFactory should have been,
class DbDatabaseFactory{
public:
virtual IConnection* CreateConnection() = 0;
virtual ICommand* CreateCommand() = 0;
};
And also in the Factory class, you should keep pointers or references to DbDatabaseFactory,IConnection and ICommand to employ runtime polymorphism
properly.
Please find the following changes to Factory class,
class Factory{
DbDatabaseFactory* _databaseFactory;
IConnection* _connection;
ICommand* _command;
public:
Factory(DbDatabaseFactory*);
void Start(){
_connection->Connect();
if(_connection->connectionState == true){
_command->Execute("SELECT ...");
}
};
};
Factory::Factory(DbDatabaseFactory* dbFactory)
{
_databaseFactory = dbFactory;
_connection = dbFactory->CreateConnection();
_command = dbFactory->CreateCommand();
}
These changes should solve your problems.
Also modify your MsSqlDbFactory
as below
class MsSqlDbFactory: public DbDatabaseFactory{
public:
IConnection* CreateConnection(){
return new SqlDbConnection();
}
ICommand* CreateCommand(){
return new SqlDbCommand();
}
};