Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C# OOP
Hallo all together,
 
In our C++/MFC project we have the following structure:
 
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 7-May-13 22:47pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Member 9586495 at 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 at 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.
 
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 325
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 189
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 8 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100