private

Allowing access to private members

拈花ヽ惹草 提交于 2019-12-18 13:39:07
问题 This question is somewhat a continuation of this one I've posted. What I was trying to do : my point was to allow access to private members of a base class A in a derived class B , with the following restraints: what I want to access is a structure -- an std::map<> , actually --, not a method; I cannot modified the base class; base class A has no templated method I may overload as a backdoor alternative -- and I would not add such method, as it would be going against the second restraint. As

In immutable class why fields are marked as private?

折月煮酒 提交于 2019-12-18 13:36:19
问题 What is the benefit of making fields private while creating an immutable class? I have seen why while creating immutable class, fields are declared as private? but I didn't get understand anything from this post. Can anybody please explain me the same? 回答1: The best way to explain is with an example: public class Immutable { private final char[] state = "Hi Mom".getChars(); public char[] getState() { return state.clone(); } } Here we have a properly encapsulated, immutable class. Nothing can

Javascript new object (function ) vs inline invocation

时光毁灭记忆、已成空白 提交于 2019-12-18 12:35:02
问题 Is there any considerations to determine which is better practice for creating an object with private members? var object = new function () { var private = "private variable"; return { method : function () { ..dosomething with private; } } } VS var object = function () { ... }(); Basically what is the difference between using NEW here, and just invoking the function immediately after we define it? 回答1: The new operator causes the function to be invoked like a Constructor Function. I've seen

Difference between OpenMP threadprivate and private

一个人想着一个人 提交于 2019-12-18 12:15:39
问题 I am trying to parallelize a C program using OpenMP. I would like to know more about: The differences between the threadprivate directive and the private clause and In which cases we must use any of them. As far as I know, the difference is the global scope with threadprivate and the preserved value across parallel regions. I found in several examples that when a piece of code contains some global/static variables that must be privatized, these variables are included in a threadprivate list

'public static final' or 'private static final' with getter?

霸气de小男生 提交于 2019-12-18 10:02:52
问题 In Java, it's taught that variables should be kept private to enable better encapsulation, but what about static constants? This: public static final int FOO = 5; Would be equivalent in result to this: private static final int FOO = 5; ... public static getFoo() { return FOO; } But which is better practice? 回答1: There's one reason to not use a constant directly in your code. Assume FOO may change later on (but still stay constant), say to public static final int FOO = 10; . Shouldn't break

How to hide a global variable, which is visible across multiple files?

橙三吉。 提交于 2019-12-18 05:57:34
问题 I am writing a C (shared) library. It started out as a single translation unit, in which I could define a couple of static global variables, to be hidden from external modules. Now that the library has grown, I want to break the module into a couple of smaller source files. The problem is that now I have two options for the mentioned globals: Have private copies at each source file and somehow sync their values via function calls - this will get very ugly very fast. Remove the static

Confusing “override a private method”

自古美人都是妖i 提交于 2019-12-18 04:36:12
问题 I have two question on this code public class Override { private void f() { System.out.println("private f()"); } public static void main(String[] args) { Override po = new Derived(); po.f(); } } class Derived extends Override { public void f() { System.out.println("public f()"); } } /* * Output: private f() */// :~ 1) How is function f is visible on the reference of Override po; 2) Why is output "private f()" 回答1: The main method is inside class Override , so ofcourse it can see the private

Best way of declaring private variables in cocoa

心不动则不痛 提交于 2019-12-18 04:23:30
问题 I would like to know what the recommendations are for declaring private instance variables in cocoa. This question is in the context of developing apps on the iPhone. I am aware of at least three ways of declaring private variables: Declare them in the interface of the h file with the modifier @private: @interface MyClass : NSObject { @private NSObject * myPrivateVar; } Declare them in the implementation section of the m file: @implementation MyClass NSObject * myPrivateVar; Declare a

Why can a “private” method be accessed from a different instance?

允我心安 提交于 2019-12-17 23:13:26
问题 Although, this is a very basic code, it seems there is something fundamentally flawed in Java, or the JVM used by the Eclipse IDE I have used to run the code. The code runs even though it should not (I think)! The code in A.java simply displays "Hello, I am A!" Here it is: import java.lang.*; import java.util.*; class A { private void methodA() {System.out.println("Hello, I am A!");} public static void main(String[] args) { A a = new A(); a.methodA(); } } I do not understand why, after

Why would a virtual function be private?

拥有回忆 提交于 2019-12-17 21:54:43
问题 I just spotted this in some code: class Foo { [...] private: virtual void Bar() = 0; [...] } Does this have any purpose? (I am trying to port some code from VS to G++, and this caught my attention) 回答1: See this Herb Sutter article as to why you'd want to do such a thing. 回答2: This is a pure virtual function that happens to be private. This makes it so that a derived class must implement the method. In this case Bar. I think you may be confused b/c this is done to create "interfaces" in C++