Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ global functions and OOP?

Tags:

c++

oop

In C++ one can have a 'GLOBAL FUNCTION', which means it does not belong to any class. I wondered if that isn't just a violation of the basic principles of OOP?

What would be the difference with using a global function or function that is static in a class? I'm thinking the latter is more OOP oriented. But I may be wrong however...

Does it not make it harder when writing a multithreaded applicaton?

like image 970
Tony The Lion Avatar asked Dec 18 '22 00:12

Tony The Lion


1 Answers

A static function inside a class is as OO as a global function inside a module. The thing is in JAVA, you don't have the choice.

In C++, you can encapsulate your global functions inside namespaces, you don't need a dummy class to do this. This way you have modularity.

So of course you can put functions outside namespaces this way you have really global functions. But that's not very different from a JAVA kitchen sink class with a bunch of static functions. It's also bad code, but can be just ok for small projects :)

Also in C++ you have a lot of choices to have "global" function that actually are linked to a class, as operator functions, that may be for instance friends of a class.

EDIT As for multithreading, you have to worry about global variables, not functions.

like image 81
Nikko Avatar answered Dec 24 '22 00:12

Nikko