I have been playing with pointers, to understand how to use then, I tried:
baz + sizeof(char)
and the code is working correctly, I get the right value, I don't understand why.
I was expecting to work correctly with:
baz + sizeof(Rectangle)
but I get a random number when I use it. how is baz[1:0] array organized? Shouldn't it be an array of 'Rectangle' objects?
#include <iostream>
using namespace std;
class Rectangle
{
int width, height;
public:
Rectangle (int x, int y): width(x), height(y) {}
int area ()
{
return width * height;
}
};
int main(int argc, const char * argv[])
{
Rectangle obj (10,4);
Rectangle *foo, *bar, *baz;
foo = &obj;
bar = new Rectangle (5,6);
baz = new Rectangle[2] { {2,5}, {1,2}} ;
cout << "obj area: " << obj.area() << endl;
cout << "foo area: " << foo -> area() << endl;
cout << "bar area: " << (*bar).area() << endl;
cout << "bar area: " << bar -> area() << endl;
cout << "baz[0] area: " << baz[0].area() << endl;
cout << "baz[1] area: " << (baz + sizeof(char)) -> area() << endl;
}