private

Why declare variables private in a class? [closed]

大兔子大兔子 提交于 2019-11-30 08:43:31
Folks I'll start by apologising as I'm sure this has been answered elsewhere - I just can't find an answer that explains it in a way I understand! I'm doing an MSc conversion course and there are some elementary basics that I'm still struggling with this, including this one - why making a variable private is better. Say I have a Java class called Person, with a print method. I could create it and define it as such: public class Person { public String name; public String telephoneNumber; public void print() { System.out.println(name + " " + telephoneNumber); } } Then, in a main class, I could

Tuple struct constructor complains about private fields

巧了我就是萌 提交于 2019-11-30 08:02:34
问题 I am working on a basic shell interpreter to familiarize myself with Rust. While working on the table for storing suspended jobs in the shell, I have gotten stuck at the following compiler error message: error: cannot invoke tuple struct constructor with private fields [E0450] let jobs = job::JobsList(vec![]); ^~~~~~~~~~~~~ It's unclear to me what is being seen as private here. As you can see below, both of the structs are tagged with pub in my module file. So, what's the secret sauce? mod

Calling private function within the same class python

♀尐吖头ヾ 提交于 2019-11-30 07:56:50
How can i call a private function from some other function within the same class? class Foo: def __bar(arg): #do something def baz(self, arg): #want to call __bar Right now, when i do this: __bar(val) from baz(), i get this: NameError: global name '_Foo__createCodeBehind' is not defined Can someone tell me what the reason of the error is? Also, how can i call a private function from another private function? There is no implicit this-> in Python like you have in C/C++ etc. You have to call it on self . class Foo: def __bar(self, arg): #do something def baz(self, arg): self.__bar(arg) These

JavaDoc for private / protected methods? [closed]

烈酒焚心 提交于 2019-11-30 07:49:18
Should I write JavaDoc for private or protected methods? And what about private variables? I see class examples on my Java book and the private variables are JavaDoc'ed. So I can't understand if it is a good practice to JavaDoc the private (or protected ) methods. AlexWien Yes you should write JavaDoc for private methods, and even when it is only for yourself. In 3 years when you have to change the code, you will be happy that you documented it. If you leave the company, or have to work on another project, your co-workers will be happy to have a documented code. Undocumented code has much

Javascript new object (function ) vs inline invocation

 ̄綄美尐妖づ 提交于 2019-11-30 07:25:33
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? The new operator causes the function to be invoked like a Constructor Function . I've seen that pattern before, but I don't see any benefits of using it. The purpose of the new operator is to create

Objective-c: why private ivars are not hidden from the outside access when using KVC

泪湿孤枕 提交于 2019-11-30 07:15:08
问题 After trying to access ivars using KVC, I have noticed that there was no protection on private and protected ivars. It doesn't matter what I put a in front of the ivar (private or protected keyword) - an ivar is always a public ivar when using KVC method "setValue". Here is my code where all of the seven ivars and properties are changeble outside the class instance: //************ interface file ***************// @interface MyClass : NSObject { @public NSNumber *public_num; @protected

Can objects with private copy constructors be thrown?

China☆狼群 提交于 2019-11-30 07:13:11
问题 I've come across some exceptions issue that is unclear to me. In C++, when an object is thrown it is first copied to a temporary object, and the temporary object is then passed to the catching code. The copy involves the use of the object's class copy constructor. AFAIK, this means that if a class has a private copy constructor, it can't be used as an exception. However, in VS2010, the following code compiles and runs: class Except { Except(const Except& other) { i = 2; } public: int i;

Accessing private variable in Category results in linker error

喜夏-厌秋 提交于 2019-11-30 07:02:53
EDIT: I'm not going to do this, I now realize how dangerous this can be. But, the question stays for purely academic purposes. I'm trying to implement a category on NSCollectionView that will let me access the private variable _displayedItems. I need to be able to access it in my subclass. So, I've created the following category: @interface NSCollectionView (displayedItems) - (NSMutableArray *)displayedItems; @end @implementation NSCollectionView (displayedItems) - (NSMutableArray *)displayedItems { return _displayedItems; } @end ...which seems like it should work perfectly. However, when I

Difference between OpenMP threadprivate and private

≯℡__Kan透↙ 提交于 2019-11-30 06:37:46
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 and their initial values are copied into the private copies using copyin . However, is there any rule

Hiding private data members? (C++)

徘徊边缘 提交于 2019-11-30 06:19:14
问题 Is there a way to hide private data members of a C++ class away from its users, in the cpp file? I think of the private members as part of the implementation and it seems a little backwards to declare them in the header file. 回答1: The "pimpl" idiom is how this is generally handled. See http://www.gotw.ca/gotw/024.htm http://www.gotw.ca/gotw/028.htm http://herbsutter.com/gotw/_100/ (updated for C++11) 回答2: you want to use something like the PIMPL idiom http://en.wikipedia.org/wiki/Opaque