如何定义复制构造函数和释放指针

How to define copy constructor and deallocate pointer

我运行 cppcheck,结果发现我需要为此class有一个复制构造函数。在这种情况下,我不知道如何定义复制构造函数。有什么建议吗?

class Simulator{


    private:

        int xMax;// = 40; //SIZE;
        int yMax;// = 40; //xMax; // 40
        //int TTMxSize = 4000;
        //const int CarMxSize = 500;
        //const int WaitListSize = 4000;
        double base_price;// = 0.85 / 4;
        double saev_vott;// = 0.35;
        char* mode_output;// = "modeChoiceStats_supply_charge.csv";    

        vector<Car>** CarMx;//[xMax][yMax];
        vector <Station>** ChStMx;//[xMax][yMax];
        vector<int> **cellChargeCount;
            vector<int> **cellChargeTime;
        int timeTripCounts [288];           

        // Functions for program

    public:
        Simulator();
        Simulator(int fleet_size, int seed, char* inputFile);
        ~Simulator();
        bool loadParameters(char* input);
        void printParameters();
        void placeInitCars();
    bool lookForCar (int x, int y, int r, int dist, int& cn);
    void assignCar (int x, int y, int c, Trip* trp);
void setBusinessTripProbability();

        void runSimulation();
};

Simulator::~Simulator()
{
    for (int x=0; x<xMax; x++)
    {
        delete [] CarMx[x];
        delete [] ChStMx[x];
        delete [] cellChargeCount[x];
        delete [] cellChargeTime[x];
    }

    for (int x=0; x<numZonesL; x++)
        delete [] zoneSharesL[x];

    for (int x=0; x<numZonesS; x++)
        delete [] zoneSharesS[x];

    delete [] CarMx;
    delete [] ChStMx;
    delete [] cellChargeCount;
    delete [] cellChargeTime;
    delete [] zoneSharesL;
    delete [] zoneSharesS;
}

此外,我在以下函数中遇到资源泄漏错误

bool Simulator::loadParameters(char* input)
{
   FILE* inputfile;
   inputfile = fopen(input, "r");
   if (inputfile == NULL){
     cout << "Could not open "<<input<<endl;
     return false;
   }
   double inputVal = -1.0;
   char* varStr;
   char* valStr;
   char instring [80];

    while (!feof(inputfile))
    {
        fgets(instring, 80, inputfile);
        comment = instring[0];
        if (comment != '#' && comment != '\n')
        {
            varStr = strtok(instring, "=");
            valStr = strtok(NULL, "[=11=]");

        if (strcmp (varStr, "xMax") == 0) {
        inputVal = strtod(valStr, NULL);
        xMax = 4 * (int) inputVal; 
        } else if (strcmp (varStr, "yMax") == 0) {
        inputVal = strtod(valStr, NULL);
        yMax = 4 * (int) inputVal;
            }
    }
    return true; <<<<<<<<< RESOURCE LEAK: inputfile
}

此函数中可能存在泄漏:指针在分配之前未被释放。

void Simulator::setBusinessTripProbability()
{

    businessTripProbability = new double[926];
    businessTripProbability  [     0     ] =   0.0000   ;
    businessTripProbability  [     1     ] =   0.0029   ;
    businessTripProbability  [     2     ] =   0.0059   ;........... until [925]

我是 Cppcheck 开发人员。

创建复制构造函数:

 Simulator(const Simulator &sim);

如果不打算使用拷贝构造函数,最好删除:

Simulator(const Simulator &) = delete;

资源泄漏:您需要使用 fclose(inputfile)

可能的泄漏:想象一下这段代码:

Simulator simulator;
simulator.setBusinessTripPossibility();
simulator.setBusinessTripPossibility();

此处存在内存泄漏。 businessTripProbability 被分配了两次,没有重新分配。您可能有一个规则,即永远不会调用 public 方法 setBusinessTripPossibility() 两次。但以我的拙见,你不应该用这样的规则设计 classes。尝试允许任意使用 public class 接口。