c++ - How to implement pure virtual functions with different parameter structures -


i'm building class pure virtual functions called database. idea have class handles database interfaces (ie: open , close) , can used on business layers.

the database class implemented in several 'flavours' different databases, mysqldatabase , oracledatabase.

i imagined database having pure virtual methods no code - header file follows:

database.hpp

class database {      public:         database();         virtual ~database();          virtual void open(const std::string databasename) = 0;         virtual void open(const std::string databasename, const std::string username, const std::string password) = 0;         virtual void open(const std::string databasename, const std::string schema, const std::string username, const std::string password) = 0;  . <other stuff> .  } 

the 3 open variations there support different database connection requirements, simplest 1 (like sqlite3 needs filename), oracle (that needs of variables connect).

i have questions implementations (let's take oracle example):

a) shall need redeclare virtual methods again on derived class header file, like:

    class  oracledatabase : public database {      public:         oracledatabase ();         virtual ~oracledatabase ();          void open(const std::string databasename);         void open(const std::string databasename, const std::string username, const std::string password);         void open(const std::string databasename, const std::string schema, const std::string username, const std::string password); } 

b) how structure implementation of open methods in derived class (let´s take sqlite3)?

void sqlite3database::open(const std::string databasename){           ...do stuff... }  void sqlite3database::open(const std::string databasename, const std::string username, const std::string password) {           ...do stuff...     }  void sqlite3database::open(const std::string databasename, const std::string schema, const std::string username, const std::string password) {           ...do stuff...     } 

am using right strategy? i've been browsing around virtual , pure virtual strategies , think best approach problem.

any suggestions/hints?

obs: i'm coming c# world apologize if there misconception here.

for writing query functions (ie. same interface databases), pure virtual functions way go.

here, trying write open function, might want consider factory design pattern: write database withour open function; , write function such static std::unique_ptr<database> sqlite3database::open(/*...*/).

using virtual function 1 advocating not idea: anyway have 3 different functions depend on database used; , worse, mother class depends on children: add new database logging scheme, have add function prototype database.

another way go use pure virtual function (preferably protected , called constructor preserve raii; , following nvi idiom) takes argument initialization string such 1 used pdo. not same anyway database type can inferred type instantiated, idea keep single argument not have multiple versions of open

(old answer kept principles tried explain)

actually can easier: forget open, , of initialization inside sqlite3database::sqlite3database(/* ... */).

after all, there no way can open database without knowing kind of db (as have know username/password, , more: have know arguments required), there no sense in trying make virtual pure function out of this.

so, example of do:

class database {     public virtual void create(/* ... */) = 0;     // ... };  class sqlite3database : public database {     sqlite3database(string filename);     public virtual void create(/* ... */) override;     // ... };  class mysqldatabase : public database {     mysqldatabase(int host, short port, string username, string password);     public virtual void create(/* ... */) override; }; 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -