Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Private function

Reply
Thread Tools

Private function

 
 
jis
Guest
Posts: n/a
 
      02-08-2013
Hi,

I have a Class A with public function A. A calls another function B.

I want to let only public functions of class A use B. So i thought of putting
it in private of Class A.

is this the best way? or simple declaring them as static and keep it outside of
class?

New to C++. So forgive me if it is a silly q.

thanks,
jis
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-08-2013
On 2/8/2013 6:00 AM, jis wrote:
> I have a Class A with public function A. A calls another function B.


English is OK. C++ is better:

class A {
public: void functionA() { functionB(); }
private: void functionB() {}
};

>
> I want to let only public functions of class A use B. So i thought of putting
> it in private of Class A.


OK.

>
> is this the best way? or simple declaring them as static and keep it outside of
> class?


Placing them outside of class gives no protection whatsoever. Keep them
private, even if they don't need any data from class A instance, in
which case they can be private *and* static in class A:

class A {
public: void functionA() { functionB(); }
private: static void functionB() {}
// ^^^^^
};

>
> New to C++. So forgive me if it is a silly q.


Perfectly OK. That's what comp.lang.c++ is for.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      02-08-2013
On Friday, 8 February 2013 13:00:29 UTC+2, jis wrote:
> Hi,
>
>
>
> I have a Class A with public function A. A calls another function B.


We usually avoid calling A::A() as 'member function'. We call it
'constructor'. Constructor is special in sense that it returns nothing,
not even void. It does construct instances of class, objects of type A.

> I want to let only public functions of class A use B. So i thought of putting
> it in private of Class A.


That is fine to have private member functions.

> Is this the best way? or simple declaring them as static and keep it
> outside of class?


Hard to answer. It depends what you need.
Non-static member functions have passing of one parameter simply displayed
differently. Example Number class:

class Number
{
public:
Number( int n = 0 );

Number& increase();

static Number& addOne( Number& nr );

int value_;
};

Number::Number( int n )
: value_( n )
{ }

Number& Number::increase()
{
++value_;
return *this; // 'this' is technically name of special parameter.
}

Number& Number::addOne( Number& nr )
{
++nr.value_;
return nr;
}

Number& Increase( Number& nr ) // may be static
{
++nr.value_;
return nr;
}

Here the non-static member function Number::increase() does exactly same
thing as do free function Increase( Number& ) and static member function
Number::addOne( Number& ). Member functions may be made private but free
function may me made static and remove any traces of its existence from
class definition. The only problem is that it can not access private or
protected members itself.

Major difference is still the way how the call looks in code:

int main()
{
Number x( 42 );

x.increase(); // non-static member function

Number::addOne( x ); // static member function

Increase( x ); // free function
}

> New to C++. So forgive me if it is a silly q.


Not silly at all. It is the most important part of class, what functionality
is exposed to outer world and with what interface. It is design,
it is art. It is the difference between inconvenient + hard to read
and easy to use + simple to read.

I can not suggest what to take since example containing 'A's and 'B's
does not describe the real situation, that is always different.

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      02-19-2013
On Fri, 2013-02-08, Victor Bazarov wrote:
> On 2/8/2013 6:00 AM, jis wrote:
>> I have a Class A with public function A. A calls another function B.

>
> English is OK. C++ is better:
>
> class A {
> public: void functionA() { functionB(); }
> private: void functionB() {}
> };
>
>>
>> I want to let only public functions of class A use B. So i thought of putting
>> it in private of Class A.

>
> OK.
>
>>
>> is this the best way? or simple declaring them as static and keep it outside of
>> class?

>
> Placing them outside of class gives no protection whatsoever.


Well, if you can keep a function in an unnamed namespace, it's
protected from other source files. That's often good enough.

a.h:
class A {
public: void functionA();
};

a.cpp:
#include "a.h"
namespace {
void functionB() {}
}

void A::functionA() { functionB(); }

You lose the inlining, but now A is protected against functionB too.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
stilewski@gmail.com
Guest
Posts: n/a
 
      02-19-2013
Another alternative would be to use anonymous namespace, but under certain circumstances.

Example:

// A.hpp
namespace {
void anonymousFunction() {
// do sth here...
}
}

class A {
public:
void fun() { anonymousFunction(); }
};


In above example function `anonymousFunction` is visible only in file A.hpp.
As you can see, no member function is needed.
 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      02-20-2013
On Tue, 19 Feb 2013 03:44:55 -0800, stilewski wrote:

> Another alternative would be to use anonymous namespace, but under
> certain circumstances.
>
> Example:
>
> // A.hpp
> namespace {
> void anonymousFunction() {
> // do sth here...
> }
> }
>
> class A {
> public:
> void fun() { anonymousFunction(); }
> };
>
>
> In above example function `anonymousFunction` is visible only in file
> A.hpp.


And in any file that #include-s A.hpp.
Furthermore, this runs afoul of the one-definition-rule if A.hpp gets
included in two or more translation units, because in each of them A::fun
calls a *different* function (that all, confusingly enough, are called
anonymousFunction).

The correct way is, as Jorgen Grahn showed else-thread, to have the
implementation of both functions in a separate source file.

Bart v Ingen Schenau
 
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
Replacing a private function an keeping access to private variables Gregor Kofler Javascript 6 06-27-2008 10:24 PM
Re: How to keep your private files private HMV Computer Support 0 02-21-2006 04:54 PM
Public Data in Private Class or Private Data in Public Class? DaveLessnau C++ 3 05-16-2005 06:53 PM
RE: Why I use private variables (WAS: RE:"private" variablesa.k.a. name mangling?) Jeremy Bowers Python 3 01-24-2005 10:52 PM
Should 'public virtual' always become 'private virtual'? & using private inheritance qazmlp C++ 19 02-04-2004 12:37 AM



Advertisments