Hemang Purohit
Hemang Purohit

Reputation: 49

Calling private functions from another private function of the same class

Can I call private functions from another private function of the same class, for example:

Class A {
public: 
  double a; 
  double b; 
  wp(a , b);

private: 
   wp1(x);
   wp2(y);
};
A::wp(a,b){
  a = wp1(x);
}
A::wp1(x){
  x = wp2(y); }

I know that in order to access private functions you need to call them from public functions, but can I call private functions from other private functions of the same class?

Upvotes: 0

Views: 4548

Answers (4)

Bathsheba
Bathsheba

Reputation: 234635

Of course you can. You can always call a private function and access all class member data from any function within the class. That's what private does.

(Note that you can also access the private members of an instance of that class passed into a member function of that class. Although surprising at first, it's how you implement overloaded operators, copy constructors, &c.)

Upvotes: 5

xMutzelx
xMutzelx

Reputation: 586

Your example should work, if I didn't miss a detail. So to answer your question, it is possible to access a private function from another private function of the same class.

Upvotes: 0

Slava
Slava

Reputation: 44238

Private members of a class can be accessed by methods of the class and it's friends. It is irrelevant if that methods are private, public or protected by themselves.

Upvotes: 0

RithvikK
RithvikK

Reputation: 111

Yes you can because the function is within the class itself

Upvotes: 0

Related Questions