Click here to Skip to main content
15,886,840 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to get the parent proccess's path Pin
Stephen Hewitt26-Dec-09 23:09
Stephen Hewitt26-Dec-09 23:09 
AnswerRe: How to get the parent proccess's path Pin
rp_suman27-Dec-09 0:25
rp_suman27-Dec-09 0:25 
AnswerRe: How to get the parent proccess's path Pin
Bram van Kampen27-Dec-09 11:50
Bram van Kampen27-Dec-09 11:50 
GeneralRe: How to get the parent proccess's path Pin
raja jamwal31-Dec-09 8:47
raja jamwal31-Dec-09 8:47 
GeneralRe: How to get the parent proccess's path Pin
Bram van Kampen3-Jan-10 23:48
Bram van Kampen3-Jan-10 23:48 
GeneralRe: How to get the parent proccess's path Pin
raja jamwal9-Jan-10 4:06
raja jamwal9-Jan-10 4:06 
GeneralRe: How to get the parent proccess's path Pin
Bram van Kampen9-Jan-10 16:10
Bram van Kampen9-Jan-10 16:10 
Questiongood encapsulation technique ? [modified] Pin
doug2526-Dec-09 10:03
doug2526-Dec-09 10:03 
EDIT:

The solution to my problem is to declare certain member variables as protected and use get and set functions to work with them. The reason I was reluctant to do this was because I thought it would be better to be able to read from protected variables directly but the speed difference compared to using a function to read a variable is no different, or so minute that it will never affect performance in any way. This is the standard for working with encapsulated variables so it's definately better than what I was doing before and allows for proper encapsulation.

I've decided to scrap the code below, what I thought was a good solution, because it's poor style and few people would understand exactly the point of it if they were to attempt to edit code that uses it :

//#define EDITINGCODE

#ifdef EDITINGCODE
	#define PROTECTED protected
#else
	#define PROTECTED public
#endif

// Using PROTECTED allows the declaration of
// member variables that shouldn't be modified
// without using member functions
// while they can be read from
// within any scope

class Class1
{
PROTECTED:
	int var;
};


Quite often I only want to change member variables of classes using member functions and prevent these from being changed elsewhere in a program but still allow access to the variables data.

A common programming technique for this effect is to have a get function in a class that returns the value of a private / protected variable. But I figured that this technique could be inefficient when lots of accesses have to be made to those encapsulated variables because functions must be used to retrieve data.

So I've come up with a different solution that might be better. Hopefully i'm explaining ok what I want to achieve.

I want to know whether this technique is good or not ?

Basically, all I have to do is create a header file or a macro something like this :

#define FRIENDTOALL \
	friend Class1; \
	friend Class2; \
	friend int function(); \
	friend int main();


So it contains all the classes and functions in the program.
And then place the FRIENDTOALL macro in each class that contains encapsulated members.
That way it's possible to access private and protected variable members from anywhere in the program and still be able to see that they are not meant to be modified without using a class's functions.

Would it be wise to use this technique so I can see what variables are not to be modified without using member functions or are there any noticable draw backs to doing it this way ?

modified on Monday, December 28, 2009 9:46 AM

AnswerRe: good encapsulation technique ? Pin
Jimmanuel26-Dec-09 14:44
Jimmanuel26-Dec-09 14:44 
AnswerRe: good encapsulation technique ? Pin
«_Superman_»26-Dec-09 16:43
professional«_Superman_»26-Dec-09 16:43 
GeneralRe: good encapsulation technique ? [modified] Pin
doug2527-Dec-09 0:05
doug2527-Dec-09 0:05 
GeneralRe: good encapsulation technique ? Pin
Cedric Moonen27-Dec-09 1:53
Cedric Moonen27-Dec-09 1:53 
GeneralRe: good encapsulation technique ? Pin
doug2527-Dec-09 2:05
doug2527-Dec-09 2:05 
GeneralRe: good encapsulation technique ? Pin
BonshatS27-Dec-09 3:14
BonshatS27-Dec-09 3:14 
GeneralRe: good encapsulation technique ? Pin
doug2527-Dec-09 6:02
doug2527-Dec-09 6:02 
GeneralRe: good encapsulation technique ? Pin
Bram van Kampen27-Dec-09 12:03
Bram van Kampen27-Dec-09 12:03 
GeneralRe: good encapsulation technique ? Pin
doug2527-Dec-09 13:16
doug2527-Dec-09 13:16 
GeneralRe: good encapsulation technique ? Pin
Bram van Kampen27-Dec-09 14:30
Bram van Kampen27-Dec-09 14:30 
GeneralRe: good encapsulation technique ? Pin
Jimmanuel27-Dec-09 14:39
Jimmanuel27-Dec-09 14:39 
GeneralRe: good encapsulation technique ? [modified] Pin
doug2527-Dec-09 22:52
doug2527-Dec-09 22:52 
GeneralRe: good encapsulation technique ? Pin
doug2528-Dec-09 1:33
doug2528-Dec-09 1:33 
GeneralRe: good encapsulation technique ? Pin
Jimmanuel28-Dec-09 3:05
Jimmanuel28-Dec-09 3:05 
GeneralRe: good encapsulation technique ? Pin
doug2528-Dec-09 3:37
doug2528-Dec-09 3:37 
GeneralRe: good encapsulation technique ? Pin
Bram van Kampen28-Dec-09 13:05
Bram van Kampen28-Dec-09 13:05 
QuestionDisplaying cyrillic alphabet Pin
bhanu_850925-Dec-09 22:22
bhanu_850925-Dec-09 22:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.