#include <iostream>


class Something
{
public:
  Something() { some_value=42; }
  void set_value(int some_value) {this->some_value = some_value; }
  void show() { std::cout << std::dec << (int)some_value << std::endl; }
private:
  int some_value;
};


int main()
{
  // Create object
  Something nice_thing;

  // Make a pointer of type Something* but don't let it point to anything yet
  Something* somethingpointer = NULL;

  // let somethingpointer point to a Something
  somethingpointer = &nice_thing;

  // show the address of that Something
  std::cout << std::hex << (unsigned long) &nice_thing << std::endl;

  // show somethingpointer
  std::cout << (unsigned long)somethingpointer << std::endl;

  // show nice_thing
  nice_thing.show();

  // show nice_thing using the pointer
  (*somethingpointer).show();
  // the same, but written in a clearer way
  somethingpointer->show();

  // create a new Something and a pointer to it
  Something* somethingpointer2 = new Something;
  somethingpointer2->show();

  // Give both objects a new value through their pointers
  somethingpointer->set_value(5);
  somethingpointer2->set_value(7);
  somethingpointer->show();
  somethingpointer2->show();


  std::cout << "Array of pointers to dynamically created objects" << std::endl;

  // create array of Something-pointers
  Something** something_array = new Something* [10];

  // Alternative:
  // Something* something_array[10];

  // fill every array element with a pointer to a freshly created object
  for(int i=0; i<10; i++)
    something_array[i] = new Something;

  // call the show() function of each object in the array
  for(int i=0; i<10; i++)
    something_array[i] -> show();

} // main()

