private-members

a way in c++ to hide a specific function

梦想的初衷 提交于 2019-11-27 22:48:13
i have an inheritance struct A : public B , i want to hide individual functions from B, is this possible? i know the opposite is possible using using BMethod in the A declaration. cheers The using keyword can be used to change visibility struct A { void method1(); }; struct B: public A { void method2(); private: using A::method1; }; If you want to selectively hide functions from B it does not make much sense to use public inheritance in the first place. Use private inheritance & selectively bring methods from B into the scope of A: struct B{ void method1(){}; void method2(){}; }; struct A :

Private Member Access Java

我是研究僧i 提交于 2019-11-27 20:55:05
Is the private member access at the class level or at the object level. If it is at the object level, then the following code should not compile class PrivateMember { private int i; public PrivateMember() { i = 2; } public void printI() { System.out.println("i is: "+i); } public void messWithI(PrivateMember t) { t.i *= 2; } public static void main (String args[]) { PrivateMember sub = new PrivateMember(); PrivateMember obj = new PrivateMember(); obj.printI(); sub.messWithI(obj); obj.printI(); } } Please clarify if accessing the member i of obj within the messWithI() method of sub is valid As

Private members in Java inheritance

旧街凉风 提交于 2019-11-27 20:52:51
I was told that for a Java subclass it can inherit all members of its superclass. So does this mean even private members? I know it can inherit protected members. Can someone explain this to me. I am now totally confused. sgokhales No, the private member are not inherited because the scope of a private member is only limited to the class in which it is defined. Only the public and protected member are inherited. From the Java Documentation , Private Members in a Superclass A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected

Private members in CoffeeScript?

空扰寡人 提交于 2019-11-27 19:48:00
问题 Does somebody know how to make private, non-static members in CoffeeScript? Currently I'm doing this, which just uses a public variable starting with an underscore to clarify that it shouldn't be used outside of the class: class Thing extends EventEmitter constructor: (@_name) -> getName: -> @_name Putting the variable in the class makes it a static member, but how can I make it non-static? Is it even possible without getting "fancy"? 回答1: Is it even possible without getting "fancy"? Sad to

Implementing private instance variables in Javascript

こ雲淡風輕ζ 提交于 2019-11-27 19:06:20
I don't know how I've missed this for so long. I've been presuming private instance variables to work like this, but they don't. They're private (as in non-global), certainly, but the variables are shared across instances. This led to some very confusing bugs. I thought I was following the best practices implemented by some of the best libraries out there, but it seems I missed something. var Printer = (function(){ var _word; Printer = function(word){ _word = word; } _print = function(){ console.log(_word); } Printer.prototype = { print: _print } return Printer; })(); var a = new Printer("Alex

Private Variables and Methods in Python [duplicate]

不想你离开。 提交于 2019-11-27 18:55:02
Possible Duplicate: The meaning of a single- and a double-underscore before an object name in Python Which should I use _foo (an underscore) or __bar (double underscore) for private members and methods in Python? Daniel Kluev Please note that there is no such thing as "private method" in Python. Double underscore is just name mangling: >>> class A(object): ... def __foo(self): ... pass ... >>> a = A() >>> A.__dict__.keys() ['__dict__', '_A__foo', '__module__', '__weakref__', '__doc__'] >>> a._A__foo() So therefore __ prefix is useful when you need the mangling to occur, for example to not

Why make private inner class member public in Java?

∥☆過路亽.° 提交于 2019-11-27 18:45:36
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? // ... } } } Gursel Koca 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 non private class or interface, it makes sense. An example: interface EvenIterator { public

How to make instance variables private in Ruby?

别等时光非礼了梦想. 提交于 2019-11-27 18:08:52
Is there any way to make instance variables "private"(C++ or Java definition) in ruby? In other words I want following code to result in an error. class Base def initialize() @x = 10 end end class Derived < Base def x @x = 20 end end d = Derived.new Josh Lee Like most things in Ruby, instance variables aren't truly "private" and can be accessed by anyone with d.instance_variable_get :@x . Unlike in Java/C++, though, instance variables in Ruby are always private. They are never part of the public API like methods are, since they can only be accessed with that verbose getter. So if there's any

Naming convention for private fields

筅森魡賤 提交于 2019-11-27 17:14:17
问题 First, I know this question has been asked several times before and that in the end, it is mostly a matter of personal preference, but reading all the threads about the subject, some things are not clear to me. Basically, something that most people agree with at least is that public member should be PascalCased while private members should be lowerCamelCased. The matter that usually brings debate is whether or not to prefix the private members by an underscore, or anything else. Prefixing

Why can private member variable be changed by class instance?

白昼怎懂夜的黑 提交于 2019-11-27 17:12:57
问题 class TestClass { private string _privateString = "hello"; void ChangeData() { TestClass otherTestClass = new TestClass(); otherTestClass._privateString = "world"; } } This code compiles in C# and the equivalent works in PHP, but can someone explain the reason why otherTestClass._privateString can be changed here ? I would have thought an instance of a class should not be able to change a private member variable under any circumstances, and that trying to access otherTestClass._privateString