I use the Airplay SDK which is a platform for building C++ apps for smartphones. It also has a x86 simulator which uses MS Visual C++ IDE + compiler.
Now, I have this class :
namespace Fair {
class Bitmap : public Sprite {
public:
const CIw2DImage* const& getBitmapData() { return bitmapData; }; // warning: returning reference to temporary
private:
CIw2DImage* bitmapData;
};
}
I get the above warning if I build with GCC (ARM) Debug. I don't get a warning with (x86) Debug.
I asked elsewhere and I got this reply :
Because `const CIw2DImage* const' is a const pointer to const CIw2DImage, and Bitmap::bitmapData is a pointer to non-const CIw2DImage compiler automatically casts pointer to non-const to const, so here's a temporary. The following code might be generated by a "typical" compiler:
const CIw2DImage* const& getBitmapData() { const CIw2DImage* const tmp = bitmapData; return tmp; }
Probably (x86) compiler doesn't detect this problem.
You might want to remove reference symbol (&) from the prototype (why do you want use a reference in this case?)
If a compiler does that, then it's totally wrong practice..? Making the value returned more "strict" is simply at compiler-level, to prevent "abuse". (x86) doesn't detect because it doesn't "cause" the problem in the 1st case..?
I return a reference to a pointer for the sole reason to "save" 32 bits of memory, i.e. use the same block of memory as the bitmapData pointer but within a different context.
Any comments please?