Please Describe the Below function

Anyone please describe the following C++ code,i.e What is it doing

class LightingManager
{
private:
friend LightingManager & LightMgr(void);
static LightingManager sLight;
};

inline LightingManager & LightMgr(void)
{
return LightingManager::sLight;
}de]
This is an example of a friend function accessing private data inside the LightingManager class.

What it is returning is a copy of a static member of LightingManager that is also a LightingManager object. A static member of a class is globally visible to all objects created by the LightingManager class, treated as a single object shared throughout all the others.
While that might seem like it's creating an infinite self-referencing loop cataclysm, it is actually completely viable(*).

You might like to check out "linked lists" to see a useful example of self-referencing classes in action.

(*) The code itself doesn't do much beside being a demonstration of this weird fact. It might be easier to understand if you know about pointers and how the compiler might undertake translating the static keyword's instructions behind the scenes into assembly/machine code. This gets a bit beyond my field as I've never written a compiler.
Last edited on
is returning is a copy of a static member of LightingManager


It is returning a reference to the static member.
Last edited on
I have one more doubts why in some situation we define object as
class class:: object
That's naming the object explicitly. In your code, you need it because you want to name that static member in that class, you have to say class::object, otherwise you can't specify it.

In the cases where class:: is not present, it can be inferred.

The same goes for this->. You can use that to explicitly name members, but it's usually implied, so you don't need to use it when accessing class members within the class.
Last edited on
Topic archived. No new replies allowed.