المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : good encapsulation technique ? [modified]



C++ Programming
12-28-2009, 09:20 AM
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 Sunday, December 27, 2009 6:20 AM