0

Given class defined as follow:

struct A {
   std::vector<int> aList;
   A() {
     for (int i = 0; i < ARRAY_LENGTH; i++)
       aList.push_back(0);
   }
}

A argument-less contructor of A is required by other part of the program. Is it possible to have ARRAY_LENGTH varies (say read from external file/modified from gui), so that

  • Step 0: ARRAY_LENGTH = 10
  • Step 1: create objects of A, with aList.size() initalized as 10.
  • Step 2: use newly created objects of A
  • Step 3: remove all previously created objects of A
  • Step 4: Repeat step 0 to 4 with different values of ARRAY_LENGTH, until end of program

The modification of ARRAY_LENGTH should be either from external file, or modified by gui. and no other part of the program should have write-access to this value. How can I achieve this requirement?

Edit: For destroying existing objects, it would be handled by other part of program

Edit(2): I'm doing a simulation project, and aList in class A represents the possible pdf values of family size. (Say aList[0] = 0.5, aList[1] = 0.2, aList[3] = 0.3). The change of maximum family size will only happen when the program running different scenarios, which mean that I will destroy previously created objects of A to reset the simulation.

YamHon.CHAN
  • 866
  • 4
  • 20
  • 36
  • Before looking at the rest of your question, that code should be (in C++03): `A() : aList(ARRAY_LENGTH) {}` – chris Mar 21 '13 at 02:59
  • Why don't you just pass the required size into the constructor? – Tushar Mar 21 '13 at 03:01
  • I'd say pass `ARRAY_LENGTH` in through the constructor and make a new object when you want to change it (seeing as how you get rid of them all). Of course it can be adapted, but that seems the simplest way for what you described. – chris Mar 21 '13 at 03:02
  • 1
    Is it possible to create `A` objects before `main()` begins? If so, things get trickier. – aschepler Mar 21 '13 at 03:08
  • 1
    It sounds like array_length should be a static data member of A. You can even initialize it before main, then change it whenever you feel like. On the other hand, there's no safe way to destroy all pre-existing As. – dspeyer Mar 21 '13 at 04:56
  • Please be more specific with what you intend to do with aList, because if you will decrement the ARRAY_LENGTH while your program is running, how you will be handling the already populated extra items in your list. – Leafy Mar 21 '13 at 11:56

2 Answers2

0

I am uncertain about your requirement for a parameter less constructor, this seems a design weakness but here is an implementation of the static default length method suggested in the comments

struct A {
   private static DefaultLength=ARRAY_LENGTH;

   public static ResetDefault(int newDefault) {
         DefaultLength=newDefault;
   }

   std::vector<int> aList;

   A() {
     for (int i = 0; i < DefaultLength; i++)
       aList.push_back(0);
   }
}

Objects of this nature will deallocate correctly.

Elemental
  • 7,365
  • 2
  • 28
  • 33
0

One of the approaches is that you can use an external xml file to specify the value of the ARRAY_LENGTH; But this will involve overhead of including the xml file in your code and parsing it to fetch the value of variable ARRAY_LENGTH

For example your xml file can look something like this :-

< xml version="1.0" encoding="UTF-8" standalone="no" ?> 
-< root>
        < LEN_VAR ARRAY_LENGTH=10/>
< /root>

Then you will need to include and compile an xml parser for C/c++ along with your code. (Various parsers are available for free: for example http://biocpp.sourceforge.net/download/xmlParser.cpp ) You will then have to fetch the value ARRAY_LENGTH before creating the object of this class.

You can change the value in your xml code without recompiling.

Putting a notification on file for modification will let you know when your value changes, and your program can continue with the new value once you re-read it from the XML file. I am not sure of what platform you are using but you can check How do I create a file listener in linux? or How do I make my program watch for file modification in C++? for notification of file changes.

Community
  • 1
  • 1
Leafy
  • 6,631
  • 1
  • 15
  • 13
  • as I said, an external file is an option to specify the value of ARRAY_LENGTH, and what I want to achieve is to let the ARRAY_LENGTH varies when the program is running. – YamHon.CHAN Mar 21 '13 at 10:44