Public Group

C helper functions in c++

This topic is 2221 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I am trying to write a helper function that will be used across several classes. In c#, I would create a static class, allowing me to access the function(s) from anywhere, usually across multiple unrelated classes. In c++, I know I can do the same thing, but I am wondering if it would be better to just create a pure c file and do the function there. I would put it in a separate file, but it would not be contained in a class. Would I create the file as a cpp and just not declare a class (but still use a header to avoid including the whole cpp file)?

The function in question is one to compile shaders, with different classes asking for different shaders, but my question applies in general.

Share on other sites
Is there a "right way"? Like if I were asked this at a job interview, what should I say?

I do enjoy the flexibility of c++, but I'm still getting used to all the power and technicality.

Edit: The right answer probably starts with "C++ is a very flexible language..." Edited by viper110110

Share on other sites
Yes, the right way is whatever makes sense for you and your project. You don't design a multi-paradigm language, and then say that any method that isn't the OOP one is the wrong way to go about something.

Even in C++ itself, most of the standard library is a bunch of loose global functions.

As for a job interview. That would be a pretty useless question to ask. It's like asking what color of t-shirt is the right one to wear on Thursday? Stupid question with no right answer.

Share on other sites

You don't design a multi-paradigm language, and then say that any method that isn't the OOP one is the wrong way to go about something.

It's sad how much this exact mindset is pushed in school. After all the time I've spent trying to understand and use OOP, I've started to hate it. A part of me wants to join Torvalds and use pure C for everything.

Share on other sites
OOP is easy to understand, and a great way to program. It has it's pros and cons, just like any other programming method.

Try reading the free ebook "Thinking in C++". It will explain objects to you.

Share on other sites

Is there a "right way"? Like if I were asked this at a job interview, what should I say?

The right answer requires more context i would say.

If i have a bunch of related static functions that do not have any reason for a class then i do something like this (exceptions excepted):
 namespace SomethingRelevant { void something1(...); void something2(...); namespace detail { // put relevant, but "private" / "internal" helper stuff here for 'something1' to use - in case there is any such code }; } 
This is fairly common approach - not just my doing. One should always avoid/minimize namespace pollution (especially global namespace).

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5

• 11
• 16
• 9
• 10
• 13
• Forum Statistics

• Total Topics
633003
• Total Posts
3009849
• Who's Online (See full list)

There are no registered users currently online

×