Search code examples
c++protected

Accessing protected member's public method


Say I have a class Permissions which has a public method change().

class Permissions
{
public:
  change()
}

I have another class File which has a protected Permissions data member and a public method getPermissions().

class File
{
protected:
  Permissions perm;
public:
  Permissions getPermissions() { return perm; } const;
}

Now File has a derived class Directory. I am trying to call the change() method from directory.cpp using getPermissions().

I have declared:

File *temp = new File(...);

I am trying to call Permissions::change() like:

temp->perm.change()

But since perm is protected I can't do that. How can I call change() using a getter like getPermissions()?


Solution

  • Yes, you should call change() using a getter like getPermissions():

    temp->getPermissions().change();