如何在两个类之间创建指针?

时间:2016-02-18 05:28:11

标签: c++

我想上火车课和汽车课。我想在火车里面有三辆车。汽车彼此指向,所以我可以很容易地从一个到另一个。

class Train{   
 // link other car, so i can go from one to another 
 // pointer to the engine 

};
class car{   
 // cargo weight and pointer to next car and way to know we are in caboose
};

这就是我所做的:

的main.cpp

#include <iostream>
#include "Train.h"
int main()
{
    Train* engine;
    car* cars = new car();
    return 0;
}

标题文件:

#ifndef TRAIN_H
#define TRAIN_H
class Train
{
  public:
    Train()
    {
        int* space = new int [3];
    }

    ~Train();
};

class car
{
   public:
      int cargo_weight;
      int wieght;
      car()
      {
        int* number = new int [3];
      }
      ~car();
       car* Next;       
       private:     
         car* head;
         car* tail;
 };
#endif

1 个答案:

答案 0 :(得分:1)

首先,抛开:

Train(): firstCar(nullptr)
{
    int* space = new int [3];
}

space是指向int的指针。如果你想保留Car,那么做得不好,但space的花括号中只有Train只有int构造函数。它指出的三个整数都丢失了。没有删除或释放,但丢失了,没有指针告诉你在哪里找到那些3 Car s很难释放他们的记忆。这称为内存泄漏,通常被认为是一个糟糕的场景。

接下来,head同时包含tailnextCar。这使得Train既是链表元素又是链表。相当奇怪,可能没有必要。让Car充当链接列表。它有理由知道Car的链条,而每辆车都没有理由知道除了下一个,可能是之前的Car之外的任何事情。

继续......

您似乎希望在Train中最多链接三个TrainCar需要的第一件事是指向链中第一个class Train { private: Car * firstCar; public: Train(): firstCar(nullptr) // ensure that the first car point at a sentinel value // otherwise we're in for a while world of hurt later { } ~Train(); }; 的指针。

bool linkCar(Car * newcar)
{
    if (firstCar != nullptr) // if there is no first car. 
                             // See told you we'd need a sentinel!
    { 
        firstCar = newcar; // this car is the first car
    }
    else
    {
        firstCar->linkCar(newcar); //tell the first car to link to the new car
    }
}

你需要一种方法来添加汽车。

Car

这意味着linkCar需要bool linkCar(Car * newcar) { if (next != nullptr) // if this car is the last car in the train add the new car here { next = newcar; } else { // not the last car. Keep looking until we find the last car next->linkCar(newcar); } } 方法

Car::next

要做到这一点,nullptr必须在构造函数中初始化为car():next(nullptr) { }

Car

不幸的是,除非教练要求你建立一个单独链接的汽车列表,否则这种方法很糟糕。首先,您需要一堆额外的簿记来阻止您添加第四辆车。您可能需要一些额外的逻辑来释放您已经分配的所有汽车。您必须确保调用汽车释放逻辑。如果教师想要一个双向链表,那么我上面提到的最好就是一个起点。幸运的是,有很多(如果不是数千个)如何在Stack Overflow上构建双向链表的示例。

相反,请存储std::vector class Train { private: std::vector<Car> cars; public: Train() { } bool addCar(Car &newcar) { if (cars.size() < 3) { cars.push_back(newcar); } } }; s

std::vector

请注意,我们不再需要析构函数,因为我们没有任何需要破坏的东西。 car为我们处理所有这些内容。

除了自己,汽车不需要了解任何事情。不需要链接到下一辆车。不需要析构函数,因为所有class car { private: int cargo_weight; int weight; public: car() { } }; 资源都是自我管理的。

car

可能应该扩展car(int c_weight, int car_weight): cargo_weight(c_weight), weight(car_weight) { } 构造函数以设置权重,以便每辆车都可以开箱即用。

private void OnOverviewClick(object sender, RoutedEventArgs e)
{
    // for all buttons in WrapPanelGreen, reset background to default value we used
    foreach (var child in WrapPanelGreen.Children)
    {
        var b = child as Button;
        if (b != null)
        {
            SolidColorBrush mySolidColorBrush = new SolidColorBrush();
            mySolidColorBrush.Color = Color.FromArgb(255, 101, 173, 241);

            b.Background = mySolidColorBrush;
        }
    }

    // the rest of your code
}