access-specifier

Access to protected member through member-pointer: is it a hack?

时光毁灭记忆、已成空白 提交于 2019-11-26 22:35:34
问题 We all know members specified protected from a base class can only be accessed from a derived class own instance. This is a feature from the Standard, and this has been discussed on Stack Overflow multiple times: Cannot access protected member of another instance from derived type's scope ; Why can't my object access protected members of another object defined in common base class? And others. But it seems possible to walk around this restriction with member pointers, as user chtz has shown

How to create a private class method?

江枫思渺然 提交于 2019-11-26 22:28:57
问题 How come this approach of creating a private class method works: class Person def self.get_name persons_name end class << self private def persons_name "Sam" end end end puts "Hey, " + Person.get_name puts "Hey, " + Person.persons_name #=> raises "private method `persons_name' called for Person:Class (NoMethodError)" But this does not: class Person def self.get_name persons_name end private def self.persons_name "Sam" end end puts "Hey, " + Person.get_name puts "Hey, " + Person.persons_name

Private virtual method in C++

天涯浪子 提交于 2019-11-26 21:24:37
What is the advantage of making a private method virtual in C++? I have noticed this in an open source C++ project: class HTMLDocument : public Document, public CachedResourceClient { private: virtual bool childAllowed(Node*); virtual PassRefPtr<Element> createElement(const AtomicString& tagName, ExceptionCode&); }; Herb Sutter has very nicely explained it here . Guideline #2: Prefer to make virtual functions private. This lets the derived classes override the function to customize the behavior as needed, without further exposing the virtual functions directly by making them callable by

What is a good example to differentiate between fileprivate and private in Swift3

帅比萌擦擦* 提交于 2019-11-26 21:22:09
This article has been helpful in understanding the new access specifiers in Swift 3 . It also gives some examples of different usages of fileprivate and private . My question is - isn't using fileprivate on a function that is going to be used only in this file the same as using private ? fileprivate is now what private used to be in earlier Swift releases: accessible from the same source file. A declaration marked as private can now only be accessed within the lexical scope it is declared in. So private is more restrictive than fileprivate . As of Swift 4, private declarations inside a type

Why make private inner class member public in Java?

这一生的挚爱 提交于 2019-11-26 19:28:51
问题 What is the reason of declaring a member of a private inner class public in Java if it still can't be accessed outside of containing class? Or can it? public class DataStructure { // ... private class InnerEvenIterator { // ... public boolean hasNext() { // Why public? // ... } } } 回答1: If the InnerEvenIterator class does not extend any class or implement any interface, I think it is nonsense because no other class can access any instance of it. However, if it extends or implements any other

Private module methods in Ruby

与世无争的帅哥 提交于 2019-11-26 18:47:34
问题 I have a two part question Best-Practice I have an algorithm that performs some operation on a data structure using the public interface It is currently a module with numerous static methods, all private except for the one public interface method. There is one instance variable that needs to be shared among all the methods. These are the options I can see, which is the best?: Module with static ('module' in ruby) methods Class with static methods Mixin module for inclusion into the data

Why does Ruby have both private and protected methods?

三世轮回 提交于 2019-11-26 15:41:44
Before I read this article , I thought access control in Ruby worked like this: public - can be accessed by any object (e.g. Obj.new.public_method ) protected - can only be accessed from within the object itself, as well as any subclasses private - same as protected, but the method doesn't exist in subclasses However, it appears that protected and private act the same, except for the fact that you can't call private methods with an explicit receiver (i.e. self.protected_method works, but self.private_method doesn't). What's the point of this? When is there a scenario when you wouldn't want

Meaning of “Cannot reduce the visibility of the inherited method” with an interface

末鹿安然 提交于 2019-11-26 14:37:38
问题 I have two files: public interface PrintService { void print(PrintDetails details); class PrintDetails { private String printTemplate; } public interface Task { String ACTION = "print"; } } and public class A implements PrintService { void print(PrintDetails details) { System.out.println("printing: " + details); } String action = PrintService.Task.ACTION; } I thought the code looks okay, but I am getting an error in the second file for the line void print(PrintDetails details) { that states:

Private virtual method in C++

懵懂的女人 提交于 2019-11-26 12:18:00
问题 What is the advantage of making a private method virtual in C++? I have noticed this in an open source C++ project: class HTMLDocument : public Document, public CachedResourceClient { private: virtual bool childAllowed(Node*); virtual PassRefPtr<Element> createElement(const AtomicString& tagName, ExceptionCode&); }; 回答1: Herb Sutter has very nicely explained it here. Guideline #2: Prefer to make virtual functions private. This lets the derived classes override the function to customize the

What is the default access modifier?

不问归期 提交于 2019-11-26 11:15:40
I just started reading a Java book and wondered; which access modifier is the default one, if none is specified? The default visibility is known as “private package” (though you can't use this explicitly), which means the field will be accessible from inside the same package to which the class belongs. As mdma pointed out, it isn't true for interface members though, for which the default is "public". See Java's Access Specifiers The default specifier depends upon context. For classes, and interface declarations, the default is package private. This falls between protected and private, allowing