access-specifier

Rationale for difference in “default” access-specifier for a base class

…衆ロ難τιáo~ 提交于 2019-12-11 07:27:39
问题 I know that there're few differences between struct and class in C++. I also understand the reason(s) for few of the difference(s). Like this one, Members of struct are public by default ; members of class are private by default . The reason why members of struct are public by default , is to make C++-struct compatible with C-struct . And the reason why member of class are private by default , is to introduce the concept of data encapsulation (i.e enforcing object-oriented principles

Access-specifiers are not foolproof?

眉间皱痕 提交于 2019-12-10 10:59:13
问题 If I've a class like this, class Sample { private: int X; }; Then we cannot access X from outside, so this is illegal, Sample s; s.X = 10; // error - private access But we can make it accessible without editing the class ! All we need to do is this, #define private public //note this define! class Sample { private: int X; }; //outside code Sample s; s.X = 10; //no error! Working code at ideone : http://www.ideone.com/FaGpZ That means, we can change the access-specifiers by defining such

private public protected access specifiers in python

时光毁灭记忆、已成空白 提交于 2019-12-10 10:11:46
问题 Can we simulate private and protected access specifiers in python? Name Mangling eg: __var=10 can simulate private but its viable to be accessed outside easily via the object. object._className__var So is there a way we could simulate or does python a solution directly which I am not aware of? 回答1: Python does not have mandatory access control like some other languages you may be used to. The philosophy of the language is "We are all consenting adults". By convention, private attributes are

What's the access modifier of the default constructor in java?

你离开我真会死。 提交于 2019-12-08 22:46:27
问题 We all know that if we don't specifically define a constructor, the compiler inserts an invisible zero-parameter constructor. I thought its access modifier was public, but in dealing with an inner class issue, I found maybe I was wrong. Here is my code: public class Outer { protected class ProtectedInner { // adding a public constructor will solve the error in SubOuterInAnotherPackage class //public ProtectedInner() {} } } And there is a subclass of Outer in another package: public class

Accessing fileprivate and private variables in extension and another class using swift 4

与世无争的帅哥 提交于 2019-12-08 16:02:54
问题 I have been going through the recent swift docs and working out on few examples in understanding private and fileprivate keywords in swift4. I am trying to access a fileprivate and private variable in an extension of the same class and another class subclassing the class but the output is unfruitful. I'm using in the following way class privateUsageExample: UIViewController { private var priVar = false fileprivate var fPriVar = false } // usage of extension in the same class extension

Indenting issue after access specifiers in Visual Studio Express [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-08 15:48:35
问题 This question already has answers here : C++ access modifier auto indentation in Visual Studio 2010 slowly driving me crazy - can it be changed? (2 answers) Closed 3 years ago . I am using Visual Studio Express 2013 After I use an access specifier, I want Visual Studio to automatically indent my members another 4 spaces further than my access specifier; but instead, it keeps the members in line with the access specifier. Is there a way to fix this? Example: This is what is does: class MyClass

Why is Java's “protected” less protected than default? [closed]

此生再无相见时 提交于 2019-12-07 10:39:58
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . In Java, we have four access specifiers: public , protected , package-private (default), and private . This is well known and not an

Make instance methods private in runtime

ε祈祈猫儿з 提交于 2019-12-07 06:12:10
问题 I need to make some instance methods private after registering that object in another object. I don't want to freeze the object because it must remain editable, only with less functionality. And I don't want to undef the methods since they are used internally. What I need is something like: class MyClass def my_method puts "Hello" end end a = MyClass.new b = MyClass.new a.my_method #=> "Hello" a.private_instance_method(:my_method) a.my_method #=> NoMethodError b.my_method #=> "Hello" Any

Access-specifiers are not foolproof?

邮差的信 提交于 2019-12-06 11:09:24
If I've a class like this, class Sample { private: int X; }; Then we cannot access X from outside, so this is illegal, Sample s; s.X = 10; // error - private access But we can make it accessible without editing the class ! All we need to do is this, #define private public //note this define! class Sample { private: int X; }; //outside code Sample s; s.X = 10; //no error! Working code at ideone : http://www.ideone.com/FaGpZ That means, we can change the access-specifiers by defining such macros just before the class definition, or before #include <headerfile.h> , #define public private //make

Make instance methods private in runtime

让人想犯罪 __ 提交于 2019-12-05 13:17:22
I need to make some instance methods private after registering that object in another object. I don't want to freeze the object because it must remain editable, only with less functionality. And I don't want to undef the methods since they are used internally. What I need is something like: class MyClass def my_method puts "Hello" end end a = MyClass.new b = MyClass.new a.my_method #=> "Hello" a.private_instance_method(:my_method) a.my_method #=> NoMethodError b.my_method #=> "Hello" Any ideas? What's public and what's private is per class . But each object can have its own class: class Foo