考虑下面的C++代码

class B;

class A{
private:
    B* mB;
};

class B{

private:
    doSomethingImportant();

};

我们有一个包含(具有)对象B的对象A。父对象为A,子对象为B。现在,如果我想让A做B做doSomethingImportant(),我看到将A添加为B的 friend 是唯一的方法去做吧。

B类内部的friend class A。这将使A的函数能够访问B的私有(private)函数。

我发现这种方法有点怪异,因为这在Data_Hiding概念中造成了漏洞。是否有更好的方法在对象之间建立父子关系?还是这是最好的方法?

为这个问题添加我的实际动机
class elevator{
private:
    //The Lift box the elevator controls
    liftboxControlUnit & mLiftBoxCtrlUnit;

    //constructor
    elevator(int Level=1, int NoOfBanks =1 );

    //Destructor
    ~elevator();

    //Triggers the search to move to the next floor if required
    void moveLiftToNext();

public:

    //Adds request to the queue
    void addRequest(int FloorNumber){

    //Add the request to the queue. The single button outside the elevator door
    mLiftBoxCtrlUnit.addRequest(FloorNumber);

    }

    //For Emergency. Should be accessible to everyone !
    void setEmergency();
    void unsetEmergency();

};

typedef enum Direction{
    UP,
    DOWN
}direction;

class liftboxControlUnit{
private:

    //The request for various floors
    set<int> mRequestQueue;

    //The various banks for the whole system
    vector<Bank> mBanks;

    //The total number of levels. Remains the same for one building
    const int mTotalLevel;

    //Instruction to move the box to certain level
    void processRequest(){

        //Do the logic to move the box.

    }

    //can passed to the elevator
    void addRequest(int x){
        mRequestQueue.insert(x);
    }

    //Can be set by elevator class
    void setEmergency(){
        //Do the required
        //Set Emergency on all Banks
    }

    void unsetEmergency(){
        //UnsetEmegency on all banks
    }

    void emergencyListener(){
        //Listen to all the banks if emergency has been set
    }

    void BankFreeListener(){
        //Listen to the banks if any is free

        //If so then
        processRequest();
    }

public:
    //Constructor
    liftboxControlUnit(int TotalLevels, int NoOfBanks): mTotalLevel(TotalLevels){
        for(int i=0 ; i lessthan NoOfBanks; ++ i)
            mBanks.push_back(Bank(0,UP));
    }
    friend class elevator;
};

class Bank{
private:

    //The dailpad inside the bank
    dailpad & mpad;

    //Current Location
    int mPresentLevel;

    //Current direction of movement
    direction mDirection;

    //Currently moving
    bool mEngaged;

    //Manipulate the bank
    void move(int NoOfMoves){
        setEngaged();

        //Move the elevator

        unsetEngaged();
    }

    //getters
    int getPresentLevel() const;
    int getDirection() const;

    //setters
    void setPresentLevel(int);
    void setDirection(direction);

    //Manipulate the engaged flag
    bool isEngaged() const;
    bool setEngaged();
    bool unsetEngaged();

    //For emergency
    void reset();

    //Dailpad Listener
    void dailpadListener(){

    }


public:
    Bank(int StartingLevel, direction Direction): mPresentLevel(StartingLevel),
            mDirection(Direction),
            mEngaged(false),
            mpad()
    {

    }

    //For emergency . Should be available for all.
    void SetEmergency();
    void UnsetEmergency();
    bool isEmergency();

    friend class liftboxControlUnit;
};


class dailpad{

private:
    //Some DS to represent the state . probably a 2D Array.

    void renderDisplay();

public:

    //Constructor
    dailpad();

    void getCommand(int x){
        //Depending on the value we can do the following

        //Make necessary changes to the display
        renderDisplay();
    }

    friend class Bank;
};

最佳答案

IMO,对于此任务,您可能应该在 Controller 类内部嵌套“升降机”类:

class lift_controller {

    class lift_box {
        open_doors();
        close_doors();
        move_to_floor();
    };

    std::vector<lift_box> bank;
};

对于外部世界,根本没有证据表明lift_box存在。它仅与lift_controller通信,并且所有与lift_box的外部通信都通过lift_controller进行。

在这种情况下(仅lift_controller完全可以访问lift_box),(至少对我来说)似乎很清楚,lift_controller可能需要在lift_box上调用的任何操作都应该设为lift_box的公共(public)函数。要强制其他人访问lift_box,请确保lift_box的定义在private:lift_controller部分中。

编辑:我应该补充一点,您在上述问题中编辑的设计对我来说几乎没有意义。例如,您拥有银行的方向和当前水平之类的东西。除非我完全误解了银行的意思,否则对我来说这似乎是一个明显的错误-银行不在特定级别或向特定方向发展。相反,排中的每个单独的电梯都处于某个水平并且(可能)沿某个方向移动。

09-26 08:44