Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise 27: checking if standard constructor worked #6

Open
tmnvanderberg opened this issue Oct 9, 2017 · 0 comments
Open

Exercise 27: checking if standard constructor worked #6

tmnvanderberg opened this issue Oct 9, 2017 · 0 comments
Assignees

Comments

@tmnvanderberg
Copy link
Collaborator

tmnvanderberg commented Oct 9, 2017

The exercise requests the following:

bool valid() const: Returns true if the constructor could successfully do its work. If not, it returns false, in which case the return values of the other member functions are all invalid.

What would be the correct way to do this? I have it initialize to false and set it to true at the end now:

User::User()
    :
        d_valid(false)
{   
    d_userId = geteuid();                   // identify current user
    passwd *pw = getpwuid(d_userId);        // passwd file for current user

    d_name = string(pw->pw_name);           
    d_groupId  = pw->pw_gid;                
    d_homeDir = string(pw->pw_dir);
    d_realName = string(pw->pw_name);
    d_shell = string(pw->pw_shell);
  
    d_valid = true;
}

But to have all values end up invalid I would need to more code, which seems convoluted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants