private

Delphi: writing to the private ancestor's field in descendant class

大兔子大兔子 提交于 2020-01-02 01:07:36
问题 I need to fix a third-party component. This component's class has private variable which is actively used by its descendants: TThirdPartyComponentBase = class private FSomeVar: Integer; public ... end; TThirdPartyComponent = class (TThirdPartyComponentBase) protected procedure Foo; virtual; end; procedure TThirdPartyComponent.Foo; begin FSomeVar := 1; // ACCESSING PRIVATE FIELD! end; This works because both classes are in the same unit, so they're kinda "friends". But if I'll try to create a

C++ Is private really private?

时间秒杀一切 提交于 2020-01-02 00:14:33
问题 I was trying out the validity of private access specifier in C++. Here goes: Interface: // class_A.h class A { public: void printX(); private: void actualPrintX(); int x; }; Implementation: // class_A.cpp void A::printX() { actualPrintX(); } void A::actualPrintX() { std::cout << x: } I built this in to a static library (.a/.lib). We now have a class_A.h and classA.a (or classA.lib) pair. I edited class_A.h and removed the private: from it. Now in another classTester.cpp: #include "class_A.h"

Private properties vs instance variables in ARC [duplicate]

眉间皱痕 提交于 2020-01-01 19:38:10
问题 This question already has an answer here : Best way of declaring private variables in cocoa (1 answer) Closed 6 years ago . Having ARC enabled for an iOS app, if I want a class to have a private value/object, it should be better to declare this: // .m file @interface MyClass () @property (strong, nonatomic) NSString *name; @end or this?: @implementation MyClass { NSString *name; } What memory management considerations should I have? Thanks! 回答1: You can use either approach. In the first case

About private instance variables in Objective-C

≡放荡痞女 提交于 2020-01-01 19:21:28
问题 In xCode 3, I defined private instance variables in a class. When I directly access the private variables in the client codes, why does the compiler just show me a warning, not an error? The code can still run. The warning says this maybe a hard error in the future. What does the "hard error" mean? Thanks. 回答1: Hard Error means that sometime in the future the compiler will behave the way you expect it to behave (i.e., it won't compile the source file when you directly access an instance

returning reference to private vs public member

走远了吗. 提交于 2020-01-01 18:18:47
问题 I would like to know what could be reasons to provide a public access method returning a reference instead of making the member public. QPoint has methods int& rx and int& ry that let me directly manipulate the coordinates. I guess the implentation looks similar to this: public: int& rx(){return x;} private: int x; The only idea I had so far is the following: By keeping the member private and "only" providing a reference, the class can still change to use a different data type for its

Java - Difference between private and package-private enum constructor [duplicate]

那年仲夏 提交于 2019-12-31 02:42:12
问题 This question already has answers here : Why can a enum have a package-private constructor? (2 answers) Closed 4 years ago . Recently I'm quite oftenly using Enumerations. So I wonder... Is there any difference between a private Enum constructor and a enum constructor withour any visibility modifier (package-private)? 回答1: According to java docs The constructor for an enum type must be package-private or private access. but Accroding to the JLS If no access modifier is specified for the

Any performance reason to put attributes protected/private?

大憨熊 提交于 2019-12-30 07:10:50
问题 I "learned" C++ at school, but there are several things I don't know, like where or what a compiler can optimize, seems I already know that inline and const can boost a little... If performance is an important thing (gaming programming for example), does putting class attributes not public ( private or protected ) allow the compiler to make more optimized code ? Because all my previous teacher were saying was it's more "secure" or "prevent not wanted or authorized class access/behavior", but

When would I use package-private in Java? [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-30 06:36:07
问题 This question already has answers here : Pros and cons of package private classes in Java? (8 answers) Closed 6 years ago . I love access control in any language, but I find that in Java I almost never (if ever) use the package-private access modifier (or lack thereof). I realize that inner classes can be private , protected , or package-private , but outer classes can only be package-private or public . Why can an outer class be package-private but not protected ? What is the benefit of

Access private elements of object of same class

孤人 提交于 2019-12-30 03:20:07
问题 Is this legal? If not, will the following code allow this? class Foo { friend class Foo; } 回答1: That's redundant. Foo already has access to all Foo members. Two Foo objects can access each other's members. class Foo { public: int touchOtherParts(const Foo &foo) {return foo.privateparts;} private: int privateparts; }; Foo a,b; b.touchOtherParts(a); The above code will work just fine. B will access a's private data member. 回答2: Yes it is legal for an object of class Foo to access the private

Accessing “Public” methods from “Private” methods in javascript class

白昼怎懂夜的黑 提交于 2019-12-30 03:05:12
问题 Is there a way to call "public" javascript functions from "private" ones within a class? Check out the class below: function Class() { this.publicMethod = function() { alert("hello"); } privateMethod = function() { publicMethod(); } this.test = function() { privateMethod(); } } Here is the code I run: var class = new Class(); class.test(); Firebug gives this error: publicMethod is not defined: [Break on this error] publicMethod(); Is there some other way to call publicMethod() within