父 - C ++中的子关系

时间:2011-11-02 03:17:33

标签: c++ oop parent-child friend data-hiding

考虑以下C ++代码

class B;

class A{
private:
    B* mB;
};

class B{

private:
    doSomethingImportant();

};

我们有一个包含(有一个)对象B的对象A.父亲是A,孩子是B.现在,如果我想要A做B做 doSomethingImportant(),我看到添加A作为B的朋友是唯一的方法。

B类中的

friend class A这将使A的函数能够访问B的私有函数。

我发现这种方法有点奇怪,因为在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;
};

2 个答案:

答案 0 :(得分:1)

IMO,对于此任务,您可能应该将“提升盒”类嵌套在控制器类中:

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的公共职能。要强制其他人无权访问lift_box,请确保lift_box的定义位于private:的{​​{1}}部分。

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

答案 1 :(得分:0)

您似乎希望class A只能访问B中的一个私有函数B::doSomethingImportant(),而不能访问其他私有函数。

这通常意味着B::doSomethingImportant()应该是公开的。像这样,A将无法访问B的其他私人数据成员。

此外,如果您不希望其他类访问B::doSomethingImportant(),则它们不应该持有指向B的指针,而是保持指向B的接口(抽象超类)的指针,该接口不会公开{ {1}}。

或许其他类只能从B读取数据。在这种情况下,他们可以保留B::doSomethingImportant(),除非他们执行B const *,否则不允许他们拨打B::doSomethingImportant()