用C ++创建对象,如果已经构造了什么?

时间:2012-12-20 04:21:41

标签: c++ object constructor exists

我还是c ++的新手。我想阅读几个来源的消息。每个源将以4个字符ID开始数据消息。每个人也会有几条数据信息。没有任何消息具有我想从设备获得的所有信息。因此,如果我创建一个以ID作为对象名称的对象,下次收到消息时,对象是否会更新或完全重建?有没有办法在代码中调用之前检查对象是否已构造?

  class Channels{
public:
    INT8U systemID;    //0x01 Glonass, 0x02 GPS
    INT8U satID;
    INT8U GlonassNumber;
    INT8U SNR;         //signal to noise ratio
    FP64 carrierPhase; //cylces
    FP64 psuedoRange;  //milliseconds
    FP64 doppler;      //HZ cycles
    float tropDelay; //meters
    float ionoDelay; //meters
};
class BaseStation{
public:
    Channels channel[32];   //each channel object has all channel class variables in it
    int numberSatelitesTracked;
    FP64 timeUTC;
    INT16U week;
    FP64 GPStoUTCoffset;
    FP64 GLOtoUTCoffset;
    INT8S recieverTimeOffset;
    FP64 posX;   //geocentric coordinates in meters
    FP64 posY;
    FP64 posZ;
    FP64 rmsX;   //expected root mean square error of coordinates
    FP64 rmsY;
    FP64 rmsZ;
};
 if( check == SOCKET_ERROR){
            if( WSAGetLastError() != WSAEWOULDBLOCK){
                printf("base station client recieve failed with error %d \n",         WSAGetLastError());
                FreeSocketInformation(i); //shuts down client socket if no data
            }
            continue;
        }
        else{
            //recieve bytes into array
            memcpy(recvArray, SocketInfo->DataBuf.buf, SocketInfo->RecvBytes +1);
            //print recieved bytes on screen
            printf("%s \n", SocketInfo->DataBuf.buf);

        //first 4 bytes in message are base ID
            cBuffer[0] = recvArray[0];
            cBuffer[1] = recvArray[1];
            cBuffer[2] = recvArray[2];
            cBuffer[3] = recvArray[3];
            baseID = cBuffer;
        //create object with 4 char name
            BaseStation baseID;

        //test message identity and sort data
            if(recvArray[4] == 0x10 && recvArray[5] == 0xF5){
                baseID.timeUTC = combine64(recvArray[6]);
                baseID.week = combine16u(recvArray[14]);
                baseID.GPStoUTCoffset = combine64(recvArray[16]);
                baseID.GLOtoUTCoffset = combine64(recvArray[24]);
                baseID.recieverTimeOffset = recvArray[32];
                int noChannels = (check-30) /30 ;
                if (noChannels >= 32){
                    noChannels = 32;
                }
                int x = 33;
                for(int m = 0; m < noChannels; m++){   //advance reading for channel m

                    baseID.channel[m].systemID = recvArray[x];
                    x++;
                    baseID.channel[m].satID = recvArray[x];
                    x++;
                    baseID.channel[m].GlonassNumber = recvArray[x];
                    x++;
                    baseID.channel[m].SNR = recvArray[x];
                    x++;
                    baseID.channel[m].carrierPhase = combine64(recvArray[x]);
                    x = x+8;
                    baseID.channel[m].psuedoRange = combine64(recvArray[x]);
                    x = x+8;
                    baseID.channel[m].doppler = combine64(recvArray[x]);
                    x = x+10;
                }  //end of for loop to gather F5 sat data
            }  //end F5 message data


            if(recvArray[4] == 0x10 && recvArray[5] == 0xF6){
                baseID.posX = combine64(recvArray[6]);
                baseID.posY = combine64(recvArray[14]);
                baseID.posZ = combine64(recvArray[22]);
                baseID.rmsX = combine64(recvArray[30]);
                baseID.rmsY = combine64(recvArray[38]);
                baseID.rmsZ = combine64(recvArray[46]);
            } //end F6 message data

好吧所以看起来阵列对我来说可能是最好的。因此,如果我设置100个基础对象,然后使用第二个布尔数组跟踪活动数组元素,这看起来应该有效吗? (baseID添加到基础对象)

BaseStation base[100];
boolean baseActive[100];
int baseNumber;
//begin message processing------------------------------------------------------------
        //first 4 bytes in message are base ID
            cBuffer[0] = recvArray[0];
            cBuffer[1] = recvArray[1];
            cBuffer[2] = recvArray[2];
            cBuffer[3] = recvArray[3];
            string name = cBuffer;
//check for existing baseID------------------------------------------------------------
// 100 array positions
//find if base is already in use, create new if not in use
        for(baseNumber = 0; base[baseNumber].baseID != name; baseNumber++){
            //for statement increases untill it finds baseID == name
            if( baseNumber >= 100){ //baseID not currently in use
                for(int n=0; baseActive[n] == true; n++){
                    //for statement increases untill finds a false baseActive
                    baseNumber = n; //assign baseNumber to the array position
                    base[baseNumber].baseID = name; //create new baseID
                    continue;
                }
            }
        }
//check and process message data--------------------------------------------------------
        if( base[baseNumber].baseID == name){
            baseActive[baseNumber] = true;
            //test message identity and sort data
           }//end of for loop

//test connection, if no bytes recieved then connection is closed.----------------------
            if( SocketInfo->RecvBytes == 0){
                FreeSocketInformation(i); //shuts down client socket if no data
                continue;
            }
        }
    } //end of read data from socket
}

//need to add a timer to remove non sending bases from the baseActive[] array

2 个答案:

答案 0 :(得分:3)

C ++是一种静态类型语言,您需要在编译时提供对象名称 您无法在运行时创建对象名称并使用该名称创建对象。

答案 1 :(得分:1)

正如已经回答的那样,你不能在C ++中这样做。 但是,您可以通过其他方式解决您的问题。 首先,您需要将一些ID绑定到结构BaseStation的某个具体对象。您可以通过两种方式提供此链接 - 将BaseStation对象保存在关联容器中,其中键是ID,或者通过保存BaseStation对象数组(据我所知,您正在编写某种类型的微控制器代码,因此std容器可能不是可供你使用)。

第一个方法代码示例:

//id is 4 char so it can be thought as int on most systems
std::map<int, BaseStation *> baseStations;
int * id = (int*)recvArray; //this hack is for showing how you can convert 4 char to int
    //may be in your code (int id = combine32(recvArray[0])) is equvivalent
if(baseStations.find(*id) != baseStations.end()) //checking existance of object with such id
{
    //ok, exists, do nothing
}
else
    baseStations[*id] = new BaseStation(); //create new

baseStations[*id].timeUTC = combine64(recvArray[6]); //starting copying values
//other values copying

在第二种情况下,如果由于微控制器内存不足而无法使用关联容器或无法负担其libs \代码,则可以仅使用数组,但它根本不灵活并且消耗更多操作。例如:

//BaseConnection also holds field names id;
BaseConnection baseConnections[N];
int FindId(int id); //return index of element in baseConnections array with this id

BaseConnection * workingConnection = &baseConnections[FindId(combine32(recvArray[0]))];
workingConnection->timeUTC = combine64(recvArray[6]); //starting copying values
//other values copying