I have a "Database" class that has a map that stores users. There is also an "Auth" class that inherits from "Database" and gets a map of users in order to find the right user during authorization. There is also the "Admin" class, which also inherits from "Database" and can add new users. But the problem is that when inheriting "Auth" and "Admin" from "Database", two instances of "Database" are created. Accordingly, if "Admin" adds a new user, "Auth" will not know about it. Что можно сделать, чтобы не создавать две копии "Database"?
class User;
class Database
{
private:
map<int, User> users;
protected:
Database() {}
void readFileOfUsers();
auto& getUsers() { return users; }
};
class Auth : public Database
{
private:
int userID;
public:
Admin itsAdmin;
User* itsUser;
Auth() : userID(0) { readFileOfUsers(); }
void logIn(std::string login, std::string password);
};
class Admin : public Database
{
public:
void getDatabase() { readFileOfUsers(); }// ?
void addNewUser(std::string login, std::string password);
};
class User
{
private:
std::string login;
std::string password;
public:
void userActions();
};