c++ - The simplest way to create a member function returning reference to object -


i created member function returning reference object.

i can like:

class foo {   foo &ref(){     return *this;   } } 

ref returning object this pointer.

is there way else return object without using this?

explain reason:

the reason don't want use this is: pointer occupy 4b in stack whereas reference share same memory

it sounds don't this and don't want dereference pointers. how about:

class foo {   private:     int dummy;   public:     foo& ref() {         return reinterpret_cast<foo&>(dummy);     } }; static_assert(std::is_standard_layout<foo>::value,               "foo must standard-layout!"); 

because foo standard-layout class , dummy first non-static data member , there no base classes, address of dummy same of containing foo.

needless say, silly way return reference object , can't see possible justification doing way. not wanting write return *this; wanting add 2 integers without using +. makes no sense @ all.


Comments