0

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?

4
  • 2
    Did you try it? Commented May 24, 2017 at 15:06
  • 1
    Yes. What happened when you tried to run it? Commented May 24, 2017 at 15:06
  • 2
    You could get your code to compile and see for yourself. Commented May 24, 2017 at 15:06
  • 1
    "I know that in order to access private functions you need to call them from public functions" Who taught you so? Commented May 24, 2017 at 15:17

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

To clarify, the deal with private: (and also protected:) relates to a client's perspective. If you call a function from outside the class (including from a derived class) you may not have access.
0

Yes you can because the function is within the class itself

Comments

0

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.