access-modifiers

Changing Function Access Mode in Derived Class

泄露秘密 提交于 2019-11-26 12:40:22
问题 Consider the following snippet: struct Base { virtual ~Base() {} virtual void Foo() const = 0; // Public }; class Child : public Base { virtual void Foo() const {} // Private }; int main() { Child child; child.Foo(); // Won\'t work. Foo is private in this context. static_cast<Base&> (child).Foo(); // Okay. Foo is public in this context. } Is this legal C++? \"This\" being changing the virtual function\'s access mode in the derived class. 回答1: Yes, changing the access mode in derived classes

Isn&#39;t “package private” member access synonymous with the default (no-modifier) access?

二次信任 提交于 2019-11-26 12:24:00
问题 I am a little confused over the term \"package private\" that some of the documentation uses, along with the usage of \"default access.\" Aren\'t package-private and default access both synonymous with protected? 回答1: Yes, it's almost the same. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition , by a subclass of its class in another package. 回答2: The "default" access modifier (the one where none of them are

What are public, private and protected in object oriented programming?

↘锁芯ラ 提交于 2019-11-26 12:11:09
What are public, private and protected in object oriented programming? They are access modifiers and help us implement Encapsulation (or information hiding). They tell the compiler which other classes should have access to the field or method being defined. private - Only the current class will have access to the field or method. protected - Only the current class and subclasses (and sometimes also same-package classes) of this class will have access to the field or method. public - Any class can refer to the field or call the method. This assumes these keywords are used as part of a field or

Internal vs. Private Access Modifiers

时光怂恿深爱的人放手 提交于 2019-11-26 11:45:11
问题 What is the difference between the internal and private access modifiers in C#? 回答1: internal is for assembly scope (i.e. only accessible from code in the same .exe or .dll) private is for class scope (i.e. accessible only from code in the same class). 回答2: Find an explanation below. You can check this link for more details - http://www.dotnetbull.com/2013/10/public-protected-private-internal-access-modifier-in-c.html Private: - Private members are only accessible within the own type (Own

Protected access modifier in Java

我的未来我决定 提交于 2019-11-26 11:16:23
问题 I am having a little trouble understanding the protected access modifier in java (or the design behind it). I thought it meant package access and access through objects that inherit the class containing an abstract member. I wrote the following sample code. I see that the commented out line produces a compilation error if uncommented. Why can I access pro through a Second object in Second but not through a First object in Second? package first; public class First { protected void pro(){

Class variables: public access read-only, but private access read/write

旧时模样 提交于 2019-11-26 10:32:42
问题 Whoopee, not working on that socket library for the moment. I\'m trying to educate myself a little more in C++. With classes, is there a way to make a variable read-only to the public, but read+write when accessed privately? e.g. something like this: class myClass { private: int x; // this could be any type, hypothetically public: void f() { x = 10; // this is OK } } int main() { myClass temp; // I want this, but with private: it\'s not allowed cout << temp.x << endl; // this is what I want:

Can non-static methods modify static variables

試著忘記壹切 提交于 2019-11-26 10:29:34
问题 I am wondering how a non static method can modify a static variable. I know that static methods can only access other static methods and static variables. However, is the other side true? Can non-static methods access only non-static variables? For example: public class SampleClass { private static int currentCount = 0; public SampleClass() { currentCount++; } public void increaseCount() { currentCount++; } } This code compiles and I would like to know why in terms of static access privledges

Why can&#39;t I have protected interface members?

自作多情 提交于 2019-11-26 10:25:42
问题 What is the argument against declaring protected-access members on interfaces? This, for example, is invalid: public interface IOrange { public OrangePeel Peel { get; } protected OrangePips Seeds { get; } } In this example, the interface IOrange would guarantee that implementors at least provide an OrangePips instance to their inheritors. If the implementor wanted to, they could expand the scope to full public : public class NavelOrange : IOrange { public OrangePeel Peel { get { return new

When overriding a method, why can I increase access but not decrease it?

蓝咒 提交于 2019-11-26 10:15:31
Why does Java specify that the access specifier for an overriding method can allow more, but not less, access than the overridden method? For example, a protected instance method in the superclass can be made public, but not private, in the subclass. It's a fundamental principle in OOP: the child class is a fully-fledged instance of the parent class, and must therefore present at least the same interface as the parent class. Making protected/public things less visible would violate this idea; you could make child classes unusable as instances of the parent class. Maurício Linhares Imagine

Does Swift have access modifiers?

社会主义新天地 提交于 2019-11-26 09:53:46
In Objective-C instance data can be public , protected or private . For example: @interface Foo : NSObject { @public int x; @protected: int y; @private: int z; } -(int) apple; -(int) pear; -(int) banana; @end I haven't found any mention of access modifiers in the Swift reference. Is it possible to limit the visibility of data in Swift? As of Swift 3.0.1 , there are 4 levels of access , described below from the highest (least restrictive) to the lowest (most restrictive). 1. open and public Enable an entity to be used outside the defining module (target). You typically use open or public access