#Static class or not?

31 messages · Page 1 of 1 (latest)

cobalt locust
#

Currently, I am writing my term paper and I have created a class ClassRenderer which basically renders text, textures, etc. on my screen (I use SDL2.0 library). And I wonder if I should turn the class into a static class and call methods by ClassRenderer::RenderText(...) or rather use ev->RenderText(...)?

tall oysterBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

cobalt locust
#

Static class or not?

fallen bough
#

I would advise against this

#

Well, to be more precise, I would advise against this **if non-constant static member variables are involved **

#

because then you'd be introducing mutable global state in your program and that's the breeding ground for headache-inducing problems, all sorts of them

#

if you only have static member functions which don't rely on any non-constant static member variables, then I'd consider turning the class into a namespace, because those are all essentially free functions

ripe nymph
#

@cobalt locust he is right

cobalt locust
fallen bough
#

I advise against making code rely on mutable global state, wherever that state is located

#

<pedantic>there's also no such thing as a static class, this is a C# concept iirc, static in C++ can mean different things based on context but it never applies to classes</pedantic>

#

it's fine to use static members

#

but if you're turning every member function from a class into static ones, then drop the class altogether and make it a namespace

cobalt locust
#

and there will be (probably) no const variables

fallen bough
#

I understood, but terminology is only going to get more important as you go, hence the remark 😛

fallen bough
#

it's the non-const ones that are a problem if you need to store them outside the functions

cobalt locust
fallen bough
#

no worries cat_up

fallen bough
#

what happens if two functions that rely on those are called at the same time ?

#

if the global variables they access are non-const, they might be tempted to write to it, both at the same time, or one while the other is reading from it

#

that's a race condition and it makes things go boom

#

if they're const you have no such problem

#

but then again you should turn that class into a namespace

fallen bough
ripe nymph
#

if @cobalt locust your question is solved then make it !solved

cobalt locust
#

I know

#

I re-read messages qreon has sent

#

I guess I now understand what you mean @fallen bough. Thank you!