Page 1 of 1

About ZBuffer ( probably a stupid question)

Posted: January 26th, 2023, 1:38 pm
by slavaxd
In the series the ZBuffer class has an overload for the At method:

Code: Select all

	float& At(int x, int y)
	{
		assert(x >= 0);
		assert(x < width);
		assert(y >= 0);
		assert(y < height);
		return pBuffer[y * width + x];
	}
	const float& At(int x, int y) const
	{
		return const_cast<ZBuffer*>(this)->At(x, y);
	}
My question is why is it using const_cast<Zbuffer> rather than const_cast<float*>? I mean, the point of this is to return a CONST reference to the pbuffer amiright?

Re: About ZBuffer ( probably a stupid question)

Posted: May 20th, 2023, 7:46 pm
by LuisR14
the point is to remove the constness of the this pointer to be able to invoke its non-const At method (it expects/needs to work with a non-const ZBuffer)

edit: (forgot to include)
without the cast, it would become a recursive call to the function itself (instead of calling that non-const method)