Sign in to follow this  
viper110110

C helper functions in c++

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 this post


Link to post
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 this post


Link to post
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. :P

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 this post


Link to post
Share on other sites
[quote name='Daaark' timestamp='1348377591' post='4982823']
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. :P
[/quote]

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 this post


Link to post
Share on other sites
[quote name='viper110110' timestamp='1348376863' post='4982820']
Is there a "right way"? Like if I were asked this at a job interview, what should I say?
[/quote]
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):
[code]
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
};

}
[/code]
This is fairly common approach - not just my doing. One should always avoid/minimize namespace pollution (especially global namespace).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this