Private function

J

jis

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
 
V

Victor Bazarov

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
 
Ö

Öö Tiib

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.
 
J

Jorgen Grahn

English is OK. C++ is better:

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


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
 
S

stilewski

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.
 
B

Bart van Ingen Schenau

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top