Lecture 4: Other class features



Lecture 4: Other class features


4.1 friend
Allows global functions and any or all member functions of other classes access to the private parts of a class. Only use this to achieve a significant optimization or as alternative method (to subclassing) for implementing systems of objects. It breaks down the encapsulation feature of C++ classes, so it should be used with care. Subclassing and member functions are preferred. Here's where making a constructor private could make sense.
mult examples
Alternatives using member functions and inline functions. friend mechanism provides significant optimization since public interface functions do array bounds checking on accesses. Otherwise, inline functions do the job fine, and no special friend access needed for efficiency.
4.2 Arrays of class objects
Special notations. Special deletion syntax we've seen.
4.3 Pointers to members
X::* syntax. Apply address operator to member accessed through type rather than object - & X::memb. Access using .* or ->*. In many cases, virtual functions obviate the necessity of function pointers.
4.4 return.C example
Both a() and b() result in only "construct X from int" being printed out. c() and d() result in construction from integer followed by copy construction. Conclusion: when a constructed value appears as the return expression, and it matches the return type of the function, the compiler uses that as instructions for constructing the return expression; a second copy construction does not get done.



0 comments: