About the ptr to int/ref to int thing: the memory allocated for the class instance of cCat holds, in your case, a pointer. But holding an int is really the same in terms of what happens to the CLASS instance; if you return a cCat from a function the out-of-scope stuff still applies whether a member of the class is a pointer or not.
So, in this case, I think an int is perfectly adequate. If you had issues with returning the cCat from a routine, you might want to do a new on the whole cCat object, and return a pointer to the cCat. Like this:
cCat* GetACat(int age)
{
return new cCat(age);
}
and then the caller has to 'delete' the return value when she's done. Generally, it would be easier to use something like this, unless there is a benefit for the allocated memory:
void GetCat( cCat* myCat, int age )
{
myCat->SetAge(age);
}
- or -
void GetCatByRef( cCat& myCat, int age )
{
myCat.SetAge(age);
}
To use, you would do something like this:
cCat myCat; // local space, not allocated
GetCat( &myCat, 23 ); // in the first version
GetCatByRef( myCat, 23 ); // in the second version
As to the destructor, you are correct that, in this specific case, the memory is freed when you exit your program. Though, when you originally mentioned a leak, the same thing would apply. You could leak a TON of memory and that would be ok since you exit the program.
Still, leaking ram is like running a stop sign. Someday you will get caught. Get in the habit of watching out for leaks and that good habit will save your keester someday.
Generally, if you allocate space in your class with new, you will want to:
a) set the pointer to NULL in your constructor, and
b) delete the pointer in your destructor, and
c) watch out for the assignment operator (do not let the compiler generate one for you, it will be wrong), and
d) make a copy constructor that allocates ram for the copy (again the compiler will be happy to generate the WRONG code for you)
If you put somthing in your destructor, you should also get in the good habit of declaring the destructor 'virtual' unless you have a clear reason for not doing that. Good reasons might include a tiny object that will NEVER have a subclass and there will be MILLIONS of them, so the saved pointer is truly a benefit.
a virtual destructor in this case also has no benefit yet, but if you do it as rote you won't make a mistake of that kind in a larger program where someone makes a subclass called 'cKitten' and you have a leak again.
Just sage advice.