So I just started learning about pointer arithmetic and I was fiddling around with some of its capabilities. Once I started trying to fool around with pointer arithmetic and classes, I came to a problem. I wrote the following code below:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Cat
{
public:
Cat();
~Cat();
int GetAge() { return itsAge; }
void SetAge(int age) { itsAge = age; }
private:
int itsAge;
};
Cat::Cat()
{
}
Cat::~Cat()
{
}
int _tmain(int argc, _TCHAR* argv[])
{
Cat *Family = new Cat[5];
Family = Family + 1;
Family->SetAge(3);
cout << Family[1].GetAge()<< endl;
return 0;
}
In my mind, I'm creating a pointer called Family which will point to an array of Cat objects. This pointer will represent the address of Family[0]. Then, on the next line, I have pointer Family point to a new address by adding 1 to the pointer itself (so the compiler should take this as moving up an address slot to the next element in the array, Family[1]). Then I set the age to 3 and try and output the value of Family[1]'s age, however the answer I get is -842150451 and not 3. What am I missing?