Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

Quote:
And how can it be solved in languages like C# or Java in which global stuff is forbidden ?
You create a static class and put the static method there.
So it's accessible from wherever you need it.
 
Share this answer
 
Quote:
But the classes are too different to create a base class to store the function.
This is not a valid objection, in my opinion.


Quote:
So we decided to write "FileExists" as a global function and put it into
stdafx.cpp which is now our global function storage.
You don't need that (albeit it is not wrong) you might create a static class for the purpose.
 
Share this answer
 
Comments
sja63 8-May-13 6:04am    
"This is not a valid objection"

Do you mean similiarity of classes is not the reason to create base classes ?

If you like can you clarify your opinion ?

Thanks in advance
CPallini 8-May-13 6:25am    
I mean: if the classes share some methods (even just one) then inheritance makes sense.
After all, code-reuse is one of the very reasons of OOP.

1) It sounds like your object model may be wrong, which would be why "OO is broken". I would check that in the first instance.

With respect to how the "problem" would be solved in C#:

2) As lukeer has suggested, you could create a static method in a class, possibly a static class, related to the type of object the method deals with.
-- In the case of "FileExists" there is already a .NET method implemented precisely in this way; a static method of the File[^] class: System.IO.File.Exists[^]

3) You could create an extension method to the type of object that the function deals with.

4) You could implement an interface class and define each class as having that interface. This is particularly useful if you need the same method(s) which can behave differently, in a class-specific way. (E.g. Dispose[^]).

Regards,
Ian.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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