gpt4 book ai didi

c++ - C++ 中的父子关系

转载 作者:太空宇宙 更新时间:2023-11-04 12:15:20 28 4
gpt4 key购买 nike

考虑下面的 C++ 代码

class B;

class A{
private:
B* mB;
};

class B{

private:
doSomethingImportant();

};

我们有一个包含(有一个)对象 B 的对象 A。父对象是 A,子对象是 B。现在如果我想让 A 让 B 执行 doSomethingImportant() ,我看到添加A 作为 B 的 friend 是唯一的办法。

friend 类 A 在类 B 中。这将使 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 的定义位于 lift_controllerprivate: 部分>.

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

关于c++ - C++ 中的父子关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7975461/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com