Skip to content

Exercise 27: checking if standard constructor worked #6

Open
@tmnvanderberg

Description

@tmnvanderberg

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.

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions