如何从另一个类内部访问一个类

How do I access a class from inside another class?

本文关键字:一个 内部 另一个 访问      更新时间:2023-10-16

作业的下一部分告诉我,类RacingCar包含由类 Wheel 定义的四个轮子对象。将轮子实现为堆上的对象数组。

class RacingCar{
    int speed;
public:
    void Accelerate(int value) {
        speed = speed + value;
    }
    void Brake(int value) {
        speed = speed - value;
    }
    void Turn(int value) {
        speed = value / 4;
    }
    void Print(){
        cout << "The current KM/h of the car is: " << speed;
    }
};
class Wheel {
    int *ptrSize;
    int pressure;
public:
    Wheel() : pressure(32) {
        ptrSize = new int(30);
    }
    Wheel (int s, int p) : pressure(p) {
        ptrSize = new int(s);
    }
    ~Wheel() {
        delete ptrSize;
    }
    void pump(int amount) {
        pressure += amount;
    }
    void print() {
        cout << pressure;
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    Wheel *heapArray = new Wheel[4];
    RacingCar F1; //Creating a "Formula 1" test car
    //Test parameters
    F1.Accelerate(10);
    F1.Brake(50);
    F1.Turn(180);
    F1.Print(); //Checks to see if the car changes actually worked
    getch();
    delete [] heapArray; //Delete when you're done
    return 0;
}

构建这样的类,然后在RacingCar内部访问它是否理想?还是有更好的方法?否则,我看不到在堆上创建它的方法。

将轮子实现为堆上的对象数组。

真的有点毫无意义,但这是一种练习,所以...

作业告诉我,"赛车"类包含由类轮定义的四个轮子对象。

当你的作业说"包含四个轮子对象"时,你可以将此要求翻译成"轮子对象应该是我的类的成员"。

我假设你的教练希望你在赛车类的ctor中初始化轮子对象数组,然后在赛车类的dtor中释放(delete[])它。

但是请注意,执行此操作的正确方法是:

class Wheel;
class RacingCar {
...
std::vector<Wheel> wheels; // if you need a variable amount of wheels
Wheel wheels[4]; // if you need exactly 4 wheels.
...

即使你真的必须在堆上分配 Wheel 对象,你仍然不会使用 delete[] ,但最好使用 boost::scoped_array 这样的工具。


让我们充实一下,因为我们想要完整的答案,即使是"家庭作业"问题,对吧?

如上所述,建模包含通常是通过类的成员完成的,尽管在这种情况下,类的成员可能是某种数组。

鉴于在堆上分配 Wheel 的要求(虽然对于玩具示例没有意义,但在成员场景中有很多合法的用例可以在堆上分配类的对象)——我会说以下解决方案是好的风格:

  • 这为您提供了一个正好包含 4 个堆分配对象的数组。您不需要显式释放这些内容:

    class RacingCar {
    ...
    boost::scoped_array<Wheel> wheels;
    ...
    RacingCar()
    : wheels(new Wheel[4])
    { }
    ...
    
  • 这使用 4 个单独的成员,这在某些用例中可能有意义,在这些用例中,您拥有同一类的成员,但不要统一使用它们:

    class RacingCar {
    ...
    boost::scoped_ptr<Wheel> front_left;
    boost::scoped_ptr<Wheel> front_right;
    boost::scoped_ptr<Wheel> rear_left;
    boost::scoped_ptr<Wheel> rear_right;
    ...
    RacingCar()
    : front_left(new Wheel)
    , front_right(new Wheel)
    , rear_left(new Wheel)
    , rear_right(new Wheel)
    { }
    
  • 如果要使用可变大小,则需要执行以下操作:

    class RacingCar {
    ...
    boost::ptr_vector<Wheel> wheels;
    ...
    RacingCar() {
      for(size_t i=0; i<4; ++i) {
        wheels.push_back(new Wheel);
      }
    }
    ...
    
  • 最后,如果你没有提升但普通的C++,我会这样做:(哦,请注意,在第一个版本中我忘记添加复制 ctor 运算符。这就是您不弄乱原始指针并删除的原因。你永远会忘记sth。;-)

    class RacingCar {
    ...
    Wheel* wheels;
    ...
    RacingCar()
    : wheels(new Wheel[4])
    { }
    ~RacingCar() {
      delete[] wheels;       
    }
    private:
    // Block copy operations. These would need to be
    // implemented properly for the wheels member.
    RacingCar(RacingCar const&); // no impl.
    RacingCar& operator=(RacingCar const&); // no impl.
    ...
    

赋值意味着 Wheel 的数组应该是类 RacingCar 的一部分,例如

class RacingCar {
public:
    ...
private: 
    ...
    Wheel *wheels;
};

你应该在构造函数中分配它,然后在析构函数中销毁它。

我认为最好

将"*heapArray"添加为RacingCar的属性并在其构造函数中创建轮子。