Consider the following class
class A
{
ClassF1 *ptrobjF1;
ClassF2 *ptrobjF2;
ClassF3 *ptrobjF3;
A()
{
ptrobjF1 = NULL;
ptrobjF2 = NULL;
ptrobjF3 = NULL;
}
};
In the above class A, i have three pointer data members for classes ClassF1,ClassF2 and ClassF3. Memory will be allocated for this classes whenever required. Probably there may arise situation that only memory for ptrobjF1 need to created (or) there may arise situaltion for all the three members memory need to be created.
Since the all the three pointers are public, it will be accessed by the end-user who is using the class.
say suppose i am creating the memory for one of the data member(ptrobjF1) in the class and i left other two data members as NULL. In such weird situation if any body access the data member outside the class as below
A obja;
(obja.ptrobjF3)->Some_function(); // Program crahses because dereferncing NULL
Is there is any design pattern to avoid accessing the Data members which is left unintialized?
Aucun commentaire:
Enregistrer un commentaire