On 23-12-2011 PM 10:17, Juha Nieminen wrote:
> ittium<> wrote:
>> How to decide whether a procedure should be private member function or
>> non-member utility function. In my opinion if a function does not
>> specify a class interface but can change "this pointer", it should be
>> declared as private, if it does not change "this pointer", it should be
>> defined as non-class utility function or static function of some utility
>> class.
>
> There's, obviously, a big difference in accessibility between a private
> method and a free function. The latter is accessible from anywhere while
> the former isn't.
yes, absolutely.
>
> Also, in the latter case if the function needs to access the private
> section of the class, you'll have to declare it as a friend.
If it accesses private data, I would also make it private.
The function is not supposed to access the private data but doing some
processing that is helping the class e.g. suppose this function do some
mathematical calculation (relevant to this class) but do not modify
*this. Other function in the class will call this method as a helper
method.
> This can
> pose some problems if you would want to make the free function local to
> the compilation unit rather than making it global.
I can define the function in a header file that can be included the
header file where class is defined. Function will be compiled in some
utility library that can be linked during linking procedure.
(Actually, I don't even
> know if you can declare a local function a friend of a class. I have never
> even tried it. Even if you can, I'm sure this would cause some ambiguity
> problems because there's no syntax to specify *which* compilation unit we
> are talking about.)
|