private-members

Python “private” name mangling and instance vs class attributes

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 02:05:12
问题 I was writing a decorator that needs to access private variables and found this discrepancy. Can anyone explain this? (Python 2.5) Naming mangling works as expected for attributes defined in the class: >>> class Tester(object): ... __foo = "hi" >>> t = Tester() >>> t._Tester__foo 'hi' Instance attributes do not work (and this is the way we are supposed to do it right?) >>> class Tester(object): ... def __init__(self): ... self.__foo = "hi" >>> t = Tester() >>> t._Tester__foo AttributeError:

Why can var_dump ascertain values of private variables, yet it can't when trying to access a single the property

柔情痞子 提交于 2019-11-30 23:29:40
I have an object that is being thrown into the session array, and I want to run a foreach on the items property. I can't seem to access it. I see that it's private, but I can't help but wonder why var_dump can show me what the property contains yet I can't read the data as it throws a fatal error? I suppose I could do some output buffering and evaluate var_dump as a string if I really have to like this but it seems like there should be a better method. Any ideas how I can access _items? The object code var_dumped from var_dump($_SESSION['PHPurchaseCart']) : object(PHPurchaseCart)#191 (4) { ["

Why can var_dump ascertain values of private variables, yet it can't when trying to access a single the property

元气小坏坏 提交于 2019-11-30 18:17:24
问题 I have an object that is being thrown into the session array, and I want to run a foreach on the items property. I can't seem to access it. I see that it's private, but I can't help but wonder why var_dump can show me what the property contains yet I can't read the data as it throws a fatal error? I suppose I could do some output buffering and evaluate var_dump as a string if I really have to like this but it seems like there should be a better method. Any ideas how I can access _items? The

Are private fields inherited by the subclass?

五迷三道 提交于 2019-11-30 15:00:42
问题 I have read that a subclass cannot inherit private fields or methods. However, in this example class SuperClass { private int n=3; int getN() { return n; } } class SubClass extends SuperClass { public static void main(String[] args) { SubClass e = new SubClass(); System.out.println("n= " + e.getN()); } } When I run main I get the output as n=3 . Which seems that SubClass is inheriting the private attribute n from SuperClass . So, please explain what's going on here. Thank you. 回答1: The

MSIL - how do you invoke a private method from MSIL?

泪湿孤枕 提交于 2019-11-30 14:13:58
I'm writing a "weak event factory" - code which converts any Delegate into a new delegate with an identical signature, but with implementing a WeakReference on the target. I'm using MSIL to avoid calls to Delegate.CreateDelegate (which performance have shown to be slow). The weak reference delegates work perfectly as long as the underlying method (the Method of the original delegate), was declared public. As soon as a private or anonymous method is used, the MSIL bombs at run time with a MethodAccessException . Using compiled expression trees, I've been able to invoke private methods, so it

Should I document my private methods? [closed]

狂风中的少年 提交于 2019-11-30 10:43:25
Private methods documentation can only be seen by who has access to the source code. Is it worth the effort spent on it? Personally, I feel that it is. Documentation is often the most useful to future developers maintaining your software - especially member documentation. Even public API documentation is only of limited use to any audience other than a developer. Document for those following - they will thank you. It definitely is. In anything but trivial software, you can make it faster to comprehend the code with the proper use of comments, even for the original author a few months later.

Why do people write private-field getters returning a non-const reference?

喜欢而已 提交于 2019-11-30 08:02:22
问题 We can all agree on public variables being bad for encapsulation and all that. However, I noticed a lot of code that does this type of stuff: class foo { private: int integer_; string someString_; // other variables public: int& integer() { return integer_; } string& someString() { return someString_; } // other "functions" } int main() { foo f; f.integer() = 10; f.someString() = "something"; return 0; } I have seen this being used in many places and I don't get why. Basically it returns a

How to create private variables within a namespace?

独自空忆成欢 提交于 2019-11-30 02:45:59
问题 For my web application, I am creating a namespace in JavaScript like so: var com = {example: {}}; com.example.func1 = function(args) { ... } com.example.func2 = function(args) { ... } com.example.func3 = function(args) { ... } I also want to create "private" (I know this doesn't exist in JS) namespace variables but am not sure what's the best design pattern to use. Would it be: com.example._var1 = null; Or would the design pattern be something else? 回答1: Closures are frequently used like this

Make a property that is read-only to the outside world, but my methods can still set

我是研究僧i 提交于 2019-11-30 02:08:33
In JavaScript (ES5+), I'm trying to achieve the following scenario: An object (of which there will be many separate instances) each with a read-only property .size that can be read from the outside via direct property read, but cannot be set from the outside. The .size property must be maintained/updated from some methods which are on the prototype (and should stay on the prototype). My API is already defined by a specification so I can't modify that (I'm working on a polyfill for an already-defined ES6 object). I'm mostly trying to prevent people from shooting themselves in the foot

c++ compiler error cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

对着背影说爱祢 提交于 2019-11-29 17:33:45
Hey Im getting an error I think has to do with copying ofstream variable from reading other posts and Ive tried to change std::ofstream outfil; to std::ofstream & outfil; but I get error reference value "outfil" requires an initializer the code is: #include <algorithm> #include <fstream> #include <iostream> #include <iterator> #include <sstream> #include <string> #include <vector> #include <cstdlib> std::vector<double> GetValues_n(const std::vector<std::string>& src, int start, int end) { std::vector<double> ret; for(int i = start; i <= end; ++i) { ret.push_back(std::strtod(src[i].c_str(),