Questions tagged [private-methods]

285 questions
546
votes
34 answers

JavaScript private methods

To make a JavaScript class with a public method I'd do something like: function Restaurant() {} Restaurant.prototype.buy_food = function(){ // something here } Restaurant.prototype.use_restroom = function(){ // something here } That way…
Wayne Kao
  • 8,125
  • 5
  • 30
  • 25
381
votes
11 answers

How do I use reflection to invoke a private method?

There are a group of private methods in my class, and I need to call one dynamically based on an input value. Both the invoking code and the target methods are in the same instance. The code looks like this: MethodInfo dynMethod =…
Jeromy Irvine
  • 11,614
  • 3
  • 39
  • 52
128
votes
8 answers

Unit testing of private methods in C++

I am in the process of writing some unit tests. In particular I want to test some private methods. So far the I have come up with using. #define private public But I am not happy with this as it will destroy all encapsulation from the point of view…
Mumbles
  • 1,654
  • 3
  • 14
  • 16
123
votes
11 answers

Why is a public const method not called when the non-const one is private?

Consider this code: struct A { void foo() const { std::cout << "const" << std::endl; } private: void foo() { std::cout << "non - const" << std::endl; } }; int main() { A a; …
Narek
  • 38,779
  • 79
  • 233
  • 389
120
votes
10 answers

Private module methods in Ruby

I have a two part question Best-Practice I have an algorithm that performs some operation on a data structure using the public interface It is currently a module with numerous static methods, all private except for the one public interface…
Daniel Beardsley
  • 19,907
  • 21
  • 66
  • 79
69
votes
4 answers

Private Variables and Methods in Python

When should I use _foo (an underscore) or __bar (double underscore) for private members and methods in Python?
62
votes
10 answers

Overriding private methods in Java

As succinctly described here, overriding private methods in Java is invalid because a parent class's private methods are "automatically final, and hidden from the derived class". My question is largely academic. How is it not a violation of…
Bill
  • 1,407
  • 1
  • 15
  • 22
51
votes
2 answers

Function privacy and unit testing Haskell

How do you deal with function visibility and unit testing in Haskell? If you export every function in a module so that the unit tests have access to them, you risk other people calling functions that should not be in the public API. I thought of…
Ralph
  • 31,584
  • 38
  • 145
  • 282
49
votes
8 answers

How can I test private members and methods of classes?

I am trying to do unit testing (using the Boost unit testing framework) on a C++ class called VariableImpl. Here are the details. class Variable { public: void UpdateStatistics (void) { // compute mean based on m_val and update m_mean; …
Jir
  • 2,985
  • 8
  • 44
  • 66
49
votes
13 answers

Private method naming convention

Is there a convention for naming the private method that I have called "_Add" here? I am not a fan of the leading underscore but it is what one of my teammates suggests. public Vector Add(Vector vector) { // check vector for null, and compare…
jason
  • 236,483
  • 35
  • 423
  • 525
29
votes
7 answers

Private Methods Over Public Methods

I was examining the StringTokenizer.java class and there were a few questions that came to mind. I noticed that the public methods which are to be used by other classes invoked some private method which did all of the work. Now, I know that one of…
lbj-ub
  • 1,425
  • 2
  • 19
  • 34
27
votes
2 answers

On a nonconst object, why won't C++ call the const version of a method with public-const and private-nonconst overloads?

class C { public: void foo() const {} private: void foo() {} }; int main() { C c; c.foo(); } MSVC 2013 doesn't like this: > error C2248: 'C::foo' : cannot access private member declared in class 'C' If I cast to a const reference,…
26
votes
2 answers

Should I test private methods using RSpec?

Is it good practice to write tests for private methods? Consider the following simple example: class Group has_many :members private def release_members members.each { |member| member.update_attributes group_id: nil } end end Would it…
tyler.amos
  • 263
  • 1
  • 3
  • 5
25
votes
8 answers

When/why to make function private in class?

When should i make a function private and why is it good idea?
Ramilol
  • 3,394
  • 11
  • 52
  • 84
25
votes
7 answers

Is there any use for a class to contain only (by default) private members in c++?

Members of a class are by default private in c++. Hence, I wonder whether there is any possible use of creating a class that has all its members (variables and functions) set by default to private. In other words, does there exist any meaningful…
mr_T
  • 2,571
  • 3
  • 22
  • 39
1
2 3
18 19