Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Private function (http://www.velocityreviews.com/forums/t957402-private-function.html)

jis 02-08-2013 11:00 AM

Private function
 
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

Victor Bazarov 02-08-2013 01:40 PM

Re: Private function
 
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

Öö Tiib 02-08-2013 01:52 PM

Re: Private function
 
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.


Jorgen Grahn 02-19-2013 11:38 AM

Re: Private function
 
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 .

stilewski@gmail.com 02-19-2013 11:44 AM

Re: Private function
 
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.

Bart van Ingen Schenau 02-20-2013 08:48 AM

Re: Private function
 
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


All times are GMT. The time now is 02:10 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.