When a pointer is incremented, it points to the next element of its type : Pointer Object « Pointer « C++






When a pointer is incremented, it points to the next element of its type


#include <iostream>
using namespace std;
class MyClass {
  int i;
public:
  MyClass() { 
    i=0; 
  }
  MyClass(int j) { 
    i=j; 
  }
  int getInt() { 
    return i; 
  }
};
int main()
{
  MyClass myObject[5] = {1, 2, 3,4,5};
  MyClass *objectPointer;
  int i;
  objectPointer = myObject; 
  for(i=0; i<5; i++) {
    cout << objectPointer->getInt() << "\n";
    objectPointer++; 
  }
  return 0;
}


           
       








Related examples in the same category

1.Incrementing and decrementing an object pointer. Incrementing and decrementing an object pointer.
2.A base pointer to access derived objectsA base pointer to access derived objects
3.Pointers to ObjectsPointers to Objects