pointer question

1
2
3
4
5
6
7
8
9
Create a class that obtains storage for integers. It should have a store() function
to insert an int value at the next available location and a get() function to read
the value in any specified location. To conserve memory, the class should use new to
get memory one chunk at a time. For example, a chunk could store 10 int values.
When store() is called for the 11th value, it must obtain another chunk of
memory using new. To the class user, the chunks are invisible; it appears as if the
integers are stored contiguously, as they would be in an array. The class, however,
maintains an array of pointers to chunks and accesses any given value by figuring
out what chunk it’s in and what number it has in the chunk.


This was an exercise.I accomplished it using two classes,one for holding array of pointers to chunks and one for holding actual array.I used two functions in the first class (which is holding pointers),as put and get.In main I used a code like;

1
2
3
4
5
6
Smartarray sa;
for(i=0;i<21;i++)
      sa.put(i,i*2);  // put(index,value to put in) i.e array[1]=2;
cout<<"\nDisplaying numbers:\n\n";
for(i=0;i<21;i++)
       cout<<sa.get(i)<<endl; 


And it worked.But I wonder iff we can perform the same task using overloaded [] operator and returning by reference.I mean,making the class user be able to use:

1
2
3
sa[5]=7;
or
cout<<sa[5];



Thanks for any suggestions..
Topic archived. No new replies allowed.