I have been reading Bruce Eckel's Thinking in C++ Volume 1. I can usually figure out what he is talking about, but in his discussion leading from C struct libraries into C++ objects, he has a part about member variables I don't follow. It has to do with using a function to retrieve a variable instead of using the member selection operator. (i.e. get_variable(&STRUCTURE) instead of STRUCTURE.variable) Eckel claims that "if you wanted to change the internal representation of CStash and thus the way the count was calculated, the function call interface allows the necessary flexibility." [quote and following code from "Chapter 4: Data Abstraction" of his book]

My question is, does his statement apply to making changes from the library user point of view or from the library developer point of view? If from the user, how is that possible? I can see that this gives the developer the option of totally changing how the number of elements in the CStash is represented with out requiring the user to change code.

Did I just answer my own question?

Thanks,

Fisher



Here is the structure headerfile:
Code:
typedef struct CStashTag {
  int size;      // Size of each space
  int quantity;  // Number of storage spaces
  int next;      // Next empty space
  // Dynamically allocated array of bytes:
  unsigned char* storage;
} CStash;

void initialize(CStash* s, int size);
void cleanup(CStash* s);
int add(CStash* s, const void* element);
void* fetch(CStash* s, int index);
int count(CStash* s);
void inflate(CStash* s, int increase);
The function definitions are as follows:
Code:
#include "CLib.h"
#include <iostream>
#include <cassert> 
using namespace std;
// Quantity of elements to add
// when increasing storage:
const int increment = 100;

void initialize(CStash* s, int sz) {
  s->size = sz;
  s->quantity = 0;
  s->storage = 0;
  s->next = 0;
}

int add(CStash* s, const void* element) {
  if(s->next >= s->quantity) //Enough space left?
    inflate(s, increment);
  // Copy element into storage,
  // starting at next empty space:
  int startBytes = s->next * s->size;
  unsigned char* e = (unsigned char*)element;
  for(int i = 0; i < s->size; i++)
    s->storage[startBytes + i] = e[i];
  s->next++;
  return(s->next - 1); // Index number
}

void* fetch(CStash* s, int index) {
  // Check index boundaries:
  assert(0 <= index);
  if(index >= s->next)
    return 0; // To indicate the end
  // Produce pointer to desired element:
  return &(s->storage[index * s->size]);
}
I omitted the functions that don't reference next.