我有两节课
class Car
{
// class Car related methods
public:
setDriversName(QString);
private:
String driversName_; // This information comes from class DriversName
//Must get the drivers name info, when a particular event in this object occurs;
}
class DriversName
{
//class DriversName related methods
String getDriversName();
private:
String driversName_;
}
所以,现在我想在两个类之间进行通信,更具体地说,我希望类在 Car Object 中发生特定事件时从类Car
中获取驱动程序的名称。DriversName
到目前为止,我有这两种方法,
C++ Message passing:
class Car
{
// class Car related methods
public:
setDriversNameObject(DriversName& );
setDriversName()
{
driversName_ = DriversName.getDriversName();
}
private:
DriversName driversName_; // So after setting the Driverclass object, i can directly
//access the driver's name, whenever the event occurs
String driversName_;
}
class DriversName
{
//class DriversName related methods
String getDriversName();
private:
DriversName driversName_;
}
或者
Qt
In mainWindow class:
connect(carObject,eventHasOccured(),this,updateDriversName());
void MainWindow::updateDriversName()
{
carObject->setDriversName(driversNameObject.getDriversName);
}
Class Car
{
Q_OBJECT
signals:
emit eventHasOccured();
public:
setDriversNameObject(DriversName& );
private:
DriversName driversName_;
}
Class DriversName
{
Q_OBJECT
String getDriversName();
private:
DriversName driversName_;
}
这两种解决方案肯定会奏效。以下是我的问题:
1)上述方法在OO原则方面是否有任何缺陷。
2) 处理这种情况的最标准方法是什么?
3)有没有其他更好的方法来处理给定的情况。
谢谢。