#include "stdafx.h"

// Simple example for people claiming "there's no difference" under the hood
// between a pointer and a reference. If you think it always compiles to the
// same code, you simply haven't lived long enough.

// Use a pointer if defined, else use a reference. Run this program twice,
// once with a pointer, once with a reference. You should not need to look
// at the disassembly to spot a difference.
#define USE_PTR

// Standard macro to compute the offset of a class member
#define OFFSET_OF(Class, Member)	(reinterpret_cast<size_t>(&reinterpret_cast<Class*>(0)->Member))

// Basic class using either a pointer or a reference member
	class Test
	{
		public:
			Test(int& core) :
#ifdef USE_PTR
				mMember(&core)
#else
				mMember(core)
#endif
			{}
			~Test()	{}

#ifdef USE_PTR
			int*	mMember;
#else
			int&	mMember;
#endif
	};

int _tmain(int argc, _TCHAR* argv[])
{
	// Computes the offset of the class member. Should be 0 in both cases, right?
#ifdef USE_PTR
	int offsetPtr = OFFSET_OF(Test, mMember);
	printf("offsetPtr: %d\n", offsetPtr);
#else
	int offsetRef = OFFSET_OF(Test, mMember);
	printf("offsetRef: %d\n", offsetRef);
#endif
	return 0;
}

