Hello all! I'm teaching myself C++ but am having a little trouble understanding pointers to objects. Take this class:
Code:
class Car {
public:
Car();
~Car();
private:
int * val1;
int * val2;
};
Car::Car() {
int val1 = new int(5);
int val2 = new int (7);
}
Car::~Car() {
delete val1;
delete val2;
}
int main() {
Car Ford;
return 0;
}
This I understand. Ford is a variable local to main, which is created on the stack, and holds two pointers to ints which are on the heap (val1 and val2).
Now onto the confusing bit:
Code:
class Car {
public:
Car();
~Car();
private:
int val1;
int val2;
};
Car::Car() {
int val1 = 5;
int val2 = 7;
}
Car::~Car() {}
int main() {
Car * Ford = new Car;
delete Ford;
return 0;
}
Isn't this the same thing? Isn't Ford a variable local to main which holds the addresses of two ints stored on the heap? That can't be right, which is why I'm confused :P.
Thanks for your help.