C ++:在构造函数中传递指针类参数

时间:2014-01-26 12:02:26

标签: c++ pointers

    class MySqlDbFactory: public DbDatabaseFactory{
      public:
             IConnection* CreateConnection(){
             return new  MySqlDbConnection();
             }

             ICommand* CreateCommand(){
             return new MySqlDbCommand();
             }
      };

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();
  } 


int main(int argc, char *argv[])
{
    MySqlDbFactory* mySQlFactory = new MySqlDbFactory();
    Factory factory = new Factory(mySQlFactory); ...

我有错误

  

请求从Factory*' to non-scalar type工厂'转换   在

  Factory factory = new Factory(mySQlFactory); line. 

你有什么建议吗?

1 个答案:

答案 0 :(得分:1)

这是:

Factory* factory = new Factory(mySQlFactory);

(请注意*之后的Factory