@fist-poster said:
@asuffield said:int &a(void) {
int b;
return b;
}int* a() {
int b;
return &b;
}I think once you have been told and understand why you shouldn't do this, that won't be a problem any more.
And if you've been told and still forget, no biggie as both of these examples produce easy to understand warnings from the compiler (I checked with gcc on linux and an MS one on 'doze), IMHO, being fastidious with compiler warnings is as important to program health as washing your hands after #2's is to your own health :)
To newbie, I've been doing C++ for 16yrs and you can 'carbon date' my source by looking at the ratio of * to &. The earliest code (during my transition from C) is full of new/delete/*, over time I started to get sick of writing if(ptr!=NULL) {...} and ancillary activities like coding big3 functions (Cline/Lomow) to correctly handle my misguided desire to explicitly manage memory... slowly & started to creep into my function signatures, from then on it was smart pointers (thanks Scott Meyers), stl, and boost. My latest code rarely has a new/delete/* and I virtually never have to think about memory management, heap corruption, pointer validity or any of that rubbish.... it just works and I can use my brain power solving the real problems.