在C 中创建对象,如果已经构造了

Creating object in C++ , what if already constructed?

本文关键字:如果 创建对象      更新时间:2023-10-16

我还是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

c 是一种静态打字的语言,您需要在编译时提供对象名称。
您不能在运行时创建对象名称,并使用该名称创建对象。

正如已经回答的,您不能在C 中这样做。但是,您可以以其他方式解决问题。首先,您需要将某些ID绑定到结构基础的某些具体对象。您可以通过两种方式提供此链接 - 通过在关联容器中持有基础对象,密钥是ID或持有碱基对象数组(据我猜,您正在编写某种MicroController代码适合您)。

第一个方法代码示例:

//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