Actually, the STL is by far the preferred way, and if basic C++ courses neglect STL, they shouldn't. STL is pretty much the core of modern C++ programs, it's not an advanced topic nor an unnecessary one.
Dynamic memory allocation will require you to create a constructor, destructor, assignment operator, and copy constructor for each structure that has dynamic memory, to make your classes robust -- otherwise, you'll be programming in horrible style which can easily lead to memory leaks.
The pre-STL way, besides being outdated, is more complex than std::vector.
Here's the minimum code you'd need for a (somewhat) safe pre-STL. If there are no exceptions thrown, it doesn't leak memory. It does NOT guarantee strong exception safety:
Code:
class Planet{
private:
int no_of_sats;
Sats * sats;
public:
float radius;
int days;
int distance;
Planet(int n = 0) : no_of_sats(n),sats(NULL){
if (n) sats = new Sats[n];
}
~Planet(){
delete[] sats;
}
Planet(Planet& p2): no_of_sats(p2.no_of_sats),radius(p2.radius),
days(p2.days), distance(p2.distance), sats(NULL){
if (no_of_sats){
sats = new Sats[no_of_sats];
for (int i = 0; i < no_of_sats; i++)
sats[i] = p2.sats[i];
}
}
Planet operator= (Planet & p2){
delete[] sats;
no_of_sats = p2.no_of_sats;
radius = p2.radius;
days = p2.days;
distance = p2.distance;
if (no_of_sats){
sats = new Sats[no_of_sats];
for (int i = 0; i < no_of_sats; i++)
sats[i] = p2.sats[i];
}
else sats = NULL;
return *this;
}
Sat & Sats(int i){
return sats[i];
}
};
I recommend against this method. You should understand it, be able to read it, and know why it works (and when it doesn't). However, this code is more complicated and not as useful as the vector implementation, which can be very, very simple. Vectors will be correctly copied, so you don't need to worry about copying or assignment, and they're freed so you don't need to worry about destructors.