dimanche 17 décembre 2017

Granting access on a function to another class without exposing it

I have a class let us call it Person:

class Person{
private:
    void move(x,y,z);
}

I have another class called PersonController:

class PersonController{
public:
    void control(){
        while(some_thing){
             //do some calculations
             controlled_person_->move(some_values); //Wrong Accessing to a private member
        }
    }
private:
    Person* controlled_person_;
}

Both Person and PersonController are part of the public interface of the library I am designing.

I want PersonController to be able to call move from Person. However, I do not want anyone to access this function (move) from the public interface.

The easy way to sovle the problem is add a friendship so PersonController can access private members of Person. However, as far as I read the friend keyword was not introduced to solve these kind of problems and using it here would be a bad practice.

  • Is this correct? Should I avoid friend here?
  • Does this mean my design is broken?
  • Any alternative suggestions?

Aucun commentaire:

Enregistrer un commentaire