static float * myVar初始化

时间:2015-04-30 04:58:35

标签: c++ pointers static initialization

使用C ++,我在类 SavingsAccount

中声明了以下静态变量
static float *waitingInQueue;

但是我无法初始化指向的数据,我只能初始化指针的地址,如下所示:float* SavingsAccount::waitingInQueue=0;

我希望这个指针的数据初始化为0.我不想使用setter因为我需要能够在不使用SavingsAccount类的对象的情况下初始化它一次。我想在函数外面执行以下语句:*waitingInQueue=0;

这是我的头文件:

#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H

class SavingsAccount
{
public:
    SavingsAccount();  
    float getInterestDue(){return *interestDue;};
    float getAmountDue() {return *amountDue;};
    int getArrivalTime() {return *arrivalTime;};
    int getFinishTime() {return *finishTime;};
    void setFinishTime(int newTime) {*finishTime=newTime;};
    float computeInterestDue();
    // compute the interest due... 
    float computeAmountDue();
    // compute the total amount due
    void waitingClient();
    // increment *arrivalTime
    void waitingProcess();
    // increment *finishTime checking if there is not another client already processing, in that case make him wait during the previous client processing
    void display();
    // display the information about 1 client
    void saveAccounts();
    // save the information of the 20 clients
    // void setWaitingInQueue(float x) {*waitingInQueue=x;}; <- can't be used
private:
    float clientTable[8];
    // information about a single client
    static float globalTable[20][8];
    static int clientCount;
    float *customerID;
    static float *arrivalTime;
    static float *finishTime;
    static const float depositType[6];
    float *depositAmount;
    static const float interestRate[12];
    float *interestDue;
    float *amountDue;
    float *PtrDepType, *PtrRate;
    static float *waitingInQueue;
};

#endif  /* SAVINGSACCOUNT_H */

这是我的.cpp

#ifndef SAVINGSACCOUNT_CPP
#define SAVINGSACCOUNT_CPP

#include<cstdlib>
#include <iostream>
#include <ctime>  // include to get the number of seconds since 1970 (srand))
#include <iomanip> // to set precision of numbers setprecision()

#ifdef WIN32
#include <windows.h> // for windows
#else
#include <unistd.h>
#endif // win32      // for unix

#include "SavingsAccount.h"

using namespace std;

float SavingsAccount::globalTable[20][8];
int SavingsAccount::clientCount=0;
const float SavingsAccount::depositType[6]={0.25, 0.5, 1, 2, 3, 5};
const float SavingsAccount::interestRate[]={1.71, 1.80, 2.07, 2.25, 2.25, 2.52, 2.70, 3.06, 3.24, 3.69, 3.60, 4.14};
float* SavingsAccount::arrivalTime;
float* SavingsAccount::finishTime;
float* SavingsAccount::waitingInQueue=0; // should be *waitingInQueue = 0;

SavingsAccount::SavingsAccount()
{
    clientCount++;
    int index;
    srand(time(NULL));          // 
    index=rand()%6;             // 
    customerID=&clientTable[0];
    arrivalTime=&clientTable[1];
    finishTime=&clientTable[2];
    PtrDepType=&clientTable[3]; // &depositType[index]
    depositAmount=&clientTable[4];
    PtrRate=&clientTable[5]; // &interestRate[N]
    interestDue=&clientTable[6];
    amountDue=&clientTable[7];
    *customerID=clientCount;
    *PtrDepType=depositType[index];
    *depositAmount=(rand()%49+1)*1000;
    if (clientCount<=10) *PtrRate=interestRate[2*index]; // N=2*index
    else *PtrRate=interestRate[2*index+1]; // N=2*index+1
}

float SavingsAccount::computeInterestDue()
{
    *interestDue=*PtrDepType*(*depositAmount)**PtrRate*(1-0.2)/100;
}

float SavingsAccount::computeAmountDue()
{
    *amountDue=*depositAmount+*interestDue;
}

void SavingsAccount::waitingClient()
{
    if (clientCount==1) *arrivalTime=0;
    else {
        int milliseconds=(rand()%6+3)*1000; // random time in milliseconds
        #ifdef WIN32   // for windows
        Sleep(milliseconds);
        #else      // for unix
        usleep(milliseconds * 1000); // nano seconds have to be multiplied by 1000
        #endif // win32
        *arrivalTime = *arrivalTime+milliseconds/1000;

    }
}

void SavingsAccount::waitingProcess()
{
    if(*finishTime < *arrivalTime)
    {
        *waitingInQueue =*arrivalTime-*finishTime;
    }
    int milliseconds=(rand()%6+2)*1000;  // random time in milliseconds
    #ifdef WIN32
    Sleep(milliseconds);
    #else
    usleep(milliseconds * 1000);
    #endif // win32
    *finishTime= *arrivalTime + milliseconds/1000 + *waitingInQueue;
}

void SavingsAccount::saveAccounts()
{
    globalTable[clientCount-1][0]=*customerID;
    globalTable[clientCount-1][1]=*arrivalTime;
    globalTable[clientCount-1][2]=*finishTime;
    globalTable[clientCount-1][3]=*PtrDepType;
    globalTable[clientCount-1][4]=*depositAmount;
    globalTable[clientCount-1][5]=*PtrRate;
    globalTable[clientCount-1][6]=*interestDue;
    globalTable[clientCount-1][7]=*amountDue;
}

void SavingsAccount::display()
{
    cout << setprecision(0) << setw(4) << right << clientTable[0];
    cout << setprecision(0) << setw(7) << right << clientTable[1];
    cout << setprecision(0) << setw(8) << right << clientTable[2];
    cout << setprecision(2) << setw(10) << right << fixed << clientTable[3];
    cout << setprecision(2) << setw(12) << right << clientTable[4];
    cout << setprecision(2) << setw(7)<< right << clientTable[5];
    cout << setprecision(2) << setw(12)<< right << clientTable[6];
    cout << setprecision(2) << setw(12)<< right << clientTable[7] << endl;
}

#endif

该程序的想法是显示每隔x秒到达的20个客户端的信息(3static float * waitingInQueue;如果需要,增加所有客户端的完成时间。

如果您有任何其他想法可以轻松模拟队列,我会感兴趣。

2 个答案:

答案 0 :(得分:0)

即使*waitingInQueue = 0;在函数外部有效,也会因为你将指针初始化为null而不正确。

做你要问的最简单的方法是这样的:

class SavingsAccount
{
    ...

private:
    static float zero;
    static float* waitingInQueue;
};

float SavingsAccount::zero = 0.0f;
float SavingsAccount::waitingInQueue = &SavingsAccount::zero;

答案 1 :(得分:0)

你可以用许多不同的方式初始化你的静态变量 - 但你应该问一个不同的问题 - &#34;我怎样才能摆脱我的静态变量?&#34;。 static变量通常表明事情不太正确,从这个角度来看,我认为你在这里遇到了一个非常大的设计问题。

我将所有静态变量分组到Branch对象中。然后让每个SavingsAccount都指向他们的Branch。并添加newAccount()Branch进行创建。然后你没有更多的静力学,一切都更整洁(更容易测试)。

指针似乎也有很多用途,其中非指针类型可以正常使用固定长度数组的情况,但是可以通过切换到std::vector<>来改进