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

When you call a.foo();, the compiler goes through overload resolution to find the best function to use. When it builds the overload set it finds void foo() const and void foo() Now, since a is not const, the non-const version is the best match, so the compiler picks void foo(). Then the access restrictions are … Read more

How can I test private members and methods of classes?

Well, unit testing should test units and ideally every class is a self-contained unit – this follows directly from the single responsibility principle. So testing private members of a class shouldn’t be necessary – the class is a black box that can be covered in a unit test as-is. On the other hand, this isn’t … Read more

Function privacy and unit testing Haskell

The usual convention is to split your module into public and private parts, i.e. module SomeModule.Internal where — … exports all private methods and then the public API module SomeModule where (export1, export2) import SomeModule.Internal Then you can import SomeModule.Internal in tests and other places where its crucial to get access to the internal implementation. … Read more

Private Variables and Methods in Python [duplicate]

Please note that there is no such thing as “private method” in Python. Double underscore is just name mangling: >>> class A(object): … def __foo(self): … pass … >>> a = A() >>> A.__dict__.keys() [‘__dict__’, ‘_A__foo’, ‘__module__’, ‘__weakref__’, ‘__doc__’] >>> a._A__foo() So therefore __ prefix is useful when you need the mangling to occur, for … Read more

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

When you call a.foo();, the compiler goes through overload resolution to find the best function to use. When it builds the overload set it finds void foo() const and void foo() Now, since a is not const, the non-const version is the best match, so the compiler picks void foo(). Then the access restrictions are … Read more