Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > private member function or non-member utility function

Reply
Thread Tools

private member function or non-member utility function

 
 
ittium
Guest
Posts: n/a
 
      12-23-2011
Group,
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.
thanks
Ittium
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      12-23-2011
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.

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. This can
pose some problems if you would want to make the free function local to
the compilation unit rather than making it global. (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.)
 
Reply With Quote
 
 
 
 
ittium
Guest
Posts: n/a
 
      12-24-2011
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.)

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      12-24-2011
ittium <> wrote:
>> 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.


That doesn't make it any less global.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      12-28-2011
On Dec 23, 4:47*pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> ittium <itt...@gmail.com> 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.


not if you make it static

> * 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. This can
> pose some problems if you would want to make the free function local to
> the compilation unit rather than making it global. (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.)


 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      01-12-2012
Nick Keighley於 2011年12月28日星期三UTC+8下午8時30分50 寫道:
> On Dec 23, 4:47*pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> > ittium <itt...@gmail.com> 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.

>
> not if you make it static
>
> > * 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. This can
> > pose some problems if you would want to make the free function local to
> > the compilation unit rather than making it global. (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.)


The private or public checking in the compile time does not matter too much..

Anyway the compile time is not the execution run time.

In higher level languages the read/write spec means more overheads in the
interpreters in the run time.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the naming convention for private member variable, andprivate and public member function? Peng Yu Python 3 09-21-2009 04:49 AM
Replacing a private function an keeping access to private variables Gregor Kofler Javascript 6 06-27-2008 10:24 PM
access private member function through a function having template type Mike C++ 9 12-29-2006 02:30 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57