user3048644
user3048644

Reputation: 93

How to assign modifiable size of array inside class in C++

I need to make the size of the array defined inside the class as situation dependent value. To clarify the point , the following code having fixed array size shows no error

class CMinimalServer : public GBDataAccess
{
public:
    DWORD IdG[30];
    VARIANT Value[30];
    WORD Quality[30];
    FILETIME Timestamp[30], ft;
    HRESULT Error[30];

But I need to make the size of the array which is '30' in the above case as an dependable value. By this I mean to say that suppose in other part of the code, I have

if (a==b)
     Number = 10;
else
     Number = 30;

The size of the array should be 10 and 30 accordingly.

But the following code shows an error

class CMinimalServer : public GBDataAccess
{
public:
    DWORD IdG[Number ];
    VARIANT Value[Number ];
    WORD Quality[Number ];
    FILETIME Timestamp[Number ], ft;
    HRESULT Error[Number ];

I tried

#define Number 16

at the top and the above code showed no error but the problem is i cannot the modify the variable in other part of the code

//// Some issue in the solution

I have modified the code as per the suggestion: I have to make functions inside the class (createTag) .

// Class definition 
class CMinimalServer : public GBDataAccess
{
public:
struct Entry
{
    DWORD IdG;
    VARIANT Value;
    WORD Quality;
    FILETIME Timestamp;
    HRESULT Error;

};

 private:
    FILETIME ft;

 void createTag()
 {
    DWORD ids[NumberOfPoints],i;
    VARIANT val;
    val.vt = VT_BOOL;

    unsigned c=0;

    for (i = 0; i<NumberOfPoints; i++)
    {
        wchar_t opcid[NumberOfPoints];
        wsprintfW(opcid, L"Item%02i", i+1); 
        val.boolVal = VARIANT_FALSE; 
        srv.GBCreateItem(&ids[i], i, opcid, OPC_READABLE|OPC_WRITEABLE, 0, &val);
        Entry.IdG[c] = ids[i]; 
        Value[c].vt= VT_BOOL; 
        c++;
    }
.....
}

//Main function
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int   nShowCmd)
{
    CMinimalServer() = default;
CMinimalServer(int number) : tab_(number){};
std::vector<Entry> tab_ = std::vector<Entry>(30);
}

The issues are:

  1. How to define idG vector array inside the CreateTag function.

Entry.idG[c] is showing error.

  1. The 'NumberOfPoints' in the loop of createTag function is also equal to 30. how to assign this value in the main function.
  2. How to make another vector array ids . can i defined it in the same struct entry and call it in createTag.

Upvotes: 0

Views: 488

Answers (5)

TNA
TNA

Reputation: 2745

If you want a non compiletime const size array, use std:vector.

Upvotes: 0

DaBuj
DaBuj

Reputation: 193

The ISO doesn't allow a variable-length array. However, there is a loophole. You can create the array with a variable as its size at runtime using dynamic memory.

For example: int x =4; int *a = new int[x];

Don't forget to use delete!

delete[] a;

I hope that helps!

Upvotes: 0

Varo
Varo

Reputation: 851

Edited : from naked pointers to shared pointers,

Dynamic memory allocation with pointers will do it,

class CMinimalServer : public GBDataAccess
{
public:
 shared_ptr<DWORD> IdG;
 shared_ptr<VARIANT> Value;
 shared_ptr<WORD> Quality;
 shared_ptr<FILETIME> Timestamp, ft;
 shared_ptr<HRESULT>* Error;

Then inside the constructor assign them the memory (define array size), like this

CMinimalServer::CMinimalServer()
{
  IdG = new DWORD(Number);
  Value = new VARIANT(Number);
  Quality = new WORD(Number);
  Timestamp = new FILETIME(Number);
}

Upvotes: -2

Trifon
Trifon

Reputation: 1151

If number is to be defined in runtime then the solution to this is to define your class as:

    class CMinimalServer : public GBDataAccess
    {
    public:
        DWORD* IdG;
        VARIANT* Value;
        WORD* Quality;
        FILETIME* Timestamp;
        FILETIME ft;
        HRESULT* Error;

        CMinimalServer(int number)
        {
             IdG = new DWORD[number];
             Value = new VARIANT[number];
          ... etc
        }

        ~CMinimalServer()
        {
             delete[] IdG;
             delete[] Value;
           ... etc
        }
   }

Upvotes: 1

galop1n
galop1n

Reputation: 8824

We have vector in C++

#include <vector>
class CMinimalServer : public GBDataAccess {
public:
    struct Entry {
        DWORD IdG;
        VARIANT Value;
        WORD Quality;
        FILETIME Timestamp;
        HRESULT Error;
    };  

    CMinimalServer() = default;
    CMinimalServer( int number ) : tab_(number) {};

private:
    FILETIME ft;
    std::vector<Entry> tab_ = std::vector<Entry>(30);
};

You can of course use a vector for each separate value if you need them contiguous and access the underlying pointer with variable.data()

Upvotes: 9

Related Questions