I have a Interface named IDBAbstractFactory
and two derived class CPostgreSQLDBAbstractFactory
and COracleDBAbstractFactory
.
I am trying to create a
std::unique_ptr<IDBAbstractFactory>
with below code which is not working
std::unique_ptr<IDBAbstractFactory> up((dbType == DATABASE_NAME::POSTGRES) ? new CPostgreSQLDBAbstractFactory : new COracleDBAbstractFactory);
The error I'm getting is
Error (active) E0042 operand types are incompatible ("CPostgreSQLDBAbstractFactory *" and "COracleDBAbstractFactory *")
Error C2446 ':': no conversion from 'COracleDBAbstractFactory *' to 'CPostgreSQLDBAbstractFactory *'
However below code works fine.
IDBAbstractFactory* pIDBAbstractFactory;
if(dbType == DATABASE_NAME::POSTGRES)
pIDBAbstractFactory = new CPostgreSQLDBAbstractFactory();
else
pIDBAbstractFactory = new COracleDBAbstractFactory();
std::unique_ptr<IDBAbstractFactory> up(pIDBAbstractFactory);
How can I create the unique_ptr with single line code. I am looking for creating the static unique_ptr.