It's hell actually. Can someone please explain in plain English why the below segments work or not?
class Hey;
class Bitmap {
public:
const Hey* const& getHey() { return hey; }; // works
const Hey* & getHey2() { return hey; }; // error C2440: 'return' : cannot convert from 'Hey *' to 'const Hey *&'
private:
Hey* hey;
};
You can't add
const
to a pointer more than one type deep which is not itselfconst
, because then you could stuff the address of aconst
variable into a non-const
pointer. Consider:Making the pointer
const
prevents this disaster a different way. Continuing the example: