How to assign array ?

class Parameters
{
  /// <summary>Information about all parameters, sorted by the parameter ID.</summary>
  public : static  ParameterDef InfoModelOne[788] ;
  public:  static  ParameterDef InfoModelTwo[816] ;
  public : static  ParameterDef InfoModelThree[788] ;  
  
};

Open in new window


During run-time we find the model of our embedded product.  Depending of the model, we use one of above arrays.  This array will be used by two different tasks.  I don't want to check the running model and use the associated array everytime the array is needed.  

I would like to get the product model, and assign array to some pointer variable and use that pointer variable whenever I need to access the array.

maybe possibilities are as follows:

ParameterDef  *  Info_Ptr;

info_Ptr = InfoModelOne;
   
            or

info_ptr = InfoModelTwo;

           or

info_ptr = InfoModelThree;

or can I create array as follows:

public : static  ParameterDef  Info [ ] ;

and then maybe I can do:      Info = InfoModelOne;    // or other two models
LVL 1
naseeamAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
You could do that within your constructor, by either determining the model there or passing information about it to the constructor, e.g.

enum ModelParameters {
   ModelOne = 0,
   ModelTwo,
   ModelThree
};

class Parameters
{
  /// <summary>Information about all parameters, sorted by the parameter ID.</summary>
 //  public : - IMO no need to make that public static
   static  ParameterDef InfoModelOne[788] ;
   static  ParameterDef InfoModelTwo[816] ;
   static  ParameterDef InfoModelThree[788] ;  

  ParameterDef* Info_Ptr;
  
public:

  Parameters(const ModelParameters mp) 
  {
     switch(mp) 
    {
      case ModelOne:

        Info_Ptr = InfoModelOne;
        break;

      case ModelOne:

        Info_Ptr = InfoModelOne;
        break;

      case ModelTwo:

        Info_Ptr = InfoModelTwo;
        break;

      case ModelThree:

        Info_Ptr = InfoModelThree;
        break;

       default:

        Info_Ptr = NULL; // treat that case accordingly
        break;
    };
  }
};
                                  

Open in new window

0
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
Why not just use a std::vector, set it's size once at runtime and that just pass it around by reference? There are many advantages of using a vector, least of which you can actually get it's size in a function it is passed to. You can't do this with a static array as they always decompose to pointers. Having multiple static arrays is wasteful and will artificially increase the size of your binary; you'll pay a cost for the one's you are not using! If I were reviewing this code as part of a peer review I would reject it as it stands.
0
 
sarabandeConnect With a Mentor Commented:
you may derive from class Parameters and handle different static arrays by means of virtual functions:

class Parameters
{
public:
       virtual size_t  getParameterInfo(ParameterDef * & infoArr) = 0;
       ....
};

class ModelOneParameters : public Parameters
{
       static ParameterDef  defArr[788];
public:
       size_t  getParameterInfo(ParameterDef * & infoArr)
       {
               infoArr = defArr;
               return sizeof(defArr)/sizeof(defArr[0]);
       }
       ...
};

// parameters.cpp
...
static ParameterDef ModelOneParameters::defArr[788] = 
     { 
          { 123, "abc", ...., },
          { 456, "xyz", ...., },
          ....
      };
static ParameterDef ModelTwoParameters::defArr[816] = 
     { 
          { 777, "HHHH", ...., },
          { 333, "OO", ...., },
          ....
      };

Open in new window


the above concept also would allow to have common (shared) parts of parameters handled in the base class and individual parts handled in the derived class.

note, definition arrays which have hundreds of elements normally is not so good a design. you might consider to load such data from a database or a resource file rather to define them in source code. if you do so, you easily could handle it with one class again and would use a dynamic container like a vector as evilrix has suggested.

Sara
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
naseeamAuthor Commented:
>> public : - IMO no need to make that public static
One of our tasks access class Parameters members as follows:

/* Before we only had one array Info. */
unsigned short infoSize = sizeof (Parameters : : Info ) ;
...
/* Another access to Info array from the same task */  
(void) _instance -> readParamList ( &Parameters : : Info [infoIndex], 0,  numParms);

I believe if it wasn't public, task wouldn't able to access member Info the Parameters class.  Am I correct ?

>> ParameterDef * Info_Ptr ;
so after  Info_Ptr is assigned one of arrays, then we use following notation to access array members ?
Info_Ptr [ 9 ],    Info_Ptr [ 3 ],    etc.

>> Why not just use a std::vector
I totally agree.  
This code base always had single array 'info' in the Parameters class.  My job is to expand ito to Three arrays.  I argued that we will waste memory but they say we have plenty of memory in this embedded product.  We are also schedule constraint.  So, this needs to get done today.  
Also, I have never used vectors in c++.  We'll need to defer this essential improvment to the future.


>> you may derive from class Parameters and handle different static arrays by means of virtual functions:

Another great idea.  Since code is setup for arrays already, decision has been made to continue using arrays.  But in future I'll fight for a better container approach.
0
 
jkrCommented:
>>I believe if it wasn't public, task wouldn't able to access member Info the Parameters class.  
>>Am I correct ?

Yes and no - you don't want to acces these values directly, you want to access

  ParameterDef* Info_Ptr;

which is assigned regarding to the device type. And for that one, add an accessor:

const  ParameterDef* const Get_Info_Ptr() const {return Info_Ptr};
ParameterDef* Get_Info_Ptr_ForReading() {return Info_Ptr};

Open in new window

Of course, the seize has to be taken into account, but that can be done via a similar enum:

enum ModelParameterSizes {
   ModelOneSize = 788,
   ModelTwoSize = 816,
   ModelThreeSize = 788
};

Open in new window

0
 
naseeamAuthor Commented:
Great support!  Coded very good solution to somewhat unclear question.

Great alternate solutions.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.