Click here to Skip to main content
15,892,161 members

Global functions vs. object orientation

sja63 asked:

Open original thread
Hallo all together,

In our C++/MFC project we have the following structure:

C++
class A {
public:
	bool FileExists(...);
};

class B {
public:
	bool FileExists(...);
};

class C {
public:
	bool FileExists(...);
};

//...


In some classes there is the same function which checks the existence of a XML file.

But the classes are too different to create a base class to store the function.

So we decided to write "FileExists" as a global function and put it into
stdafx.cpp which is now our global function storage.

My question:

The advantage of an "old" language like C++ allows the usage of
global functions and variables, with leads to its disadvantage that object orientation
is broken.

What other stategies are available to solve the upper "problem" ?

And how can it be solved in languages like C# or Java in which global stuff is forbidden ?

Best regards
Tags: C++, C#, OOP

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900