I'm currently learning C++ and I can't wrap my head around why one would define an object of a class as a pointer (i.e. Car *c = new Car; as opposed to Car c;) for no apparent reason?
Take for example:
#include <iostream>
class Car {
public:
void accelerate() { std::cout << "Accelerating!.\n"; }
};
int main() {
Car *c = new Car;
c->accelerate();
return 0;
}
Why would one do it like that when it is much more intuitive to do it like this:
#include <iostream>
class Car {
public:
void accelerate() { std::cout << "Accelerating!.\n"; }
};
int main() {
Car c;
c.accelerate();
return 0;
}
If a pointer to the object is ever needed, one could always declare one, no?