lundi 26 septembre 2022

Factory pattern with friend class in c++ [duplicate]

I am trying to implement factory pattern with private constructor and friend class. But below piece of code is getting failed. What could be the reason of failing?

class DFactory;

class Database {
public:
    virtual void CreateConnection() = 0;
    virtual ~Database() {};
};

class SQL : public Database {
private:
    SQL();
    friend class DFactory;
public:
    void CreateConnection(){}
};

// Factory class
class DFactory {
public:
    static Database* createDb() {
        return new SQL();
    }
};

// Driver program
int main() {
    Database *db = DFactory::createDb();
    db->CreateConnection();
    return 0;
}

This is the compiler error that I am getting

Could anyone help me to suggest the problem?

Aucun commentaire:

Enregistrer un commentaire