'How to store logged user in c++

i want to create login system in c++, and i've got simple login function:

bool DBProperties::loginToSystem(string identifier, string password)
{
connectToDatabase();

prep_stmt = con->prepareStatement("SELECT * FROM user WHERE identifier_code = ? AND password = ?");

prep_stmt->setString(1, identifier);
prep_stmt->setString(2, generateHashPassword(password));

res = prep_stmt->executeQuery();

if (res->rowsCount() == 1) {
    res->next();
}

delete prep_stmt;
delete con;
return false;

}

And my question is, how to store user that i get from database? Can i create object which will be visible from whole project? I need to do operates on this user from other functions.

Thanks.



Solution 1:[1]

If what you want is one user class that could have members like

user.name; user.password; user.email;

you could use a singleton pattern which would be declared globally.

this is a general outline of a singleton this website has a full explanation though. singletons. ``

// rough singleton pattern
class user
{
private:
    user(){
    //   single instance checking code here
    }
public:
    string user;
    string password;
    string email;
};
user* userObject = new user();
int main(){
    cout << userObject.user << endl;
    cout << userObject.email << endl;
}

`` singleton patterns have a major flaw though, they are inherently difficult to expand on, say you at some point wanted to cache another user you would have to create another user class.

you could put the class on the heap(so it won't be deleted when the function ends) and then pass a pointer to your user object to wherever it's needed the pointer method might be a better solution

//   something like this
class user{
user(string user, string password, string email){
user = user;
email = email;
password = password;
}
string user;
string password;
string email;
};
int myLoginFunction(string username, string password, string email, user* userObject){

userObject = new user(username, password, email);

}

you can then use that pointer to your user object to access that object with userObjectPointer->memberValue

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ican'tThinkOfAName