private-members

Why does C++ allow private members to be modified using this approach?

廉价感情. 提交于 2019-11-27 15:00:53
After seeing this question a few minutes ago, I wondered why the language designers allow it as it allows indirect modification of private data. As an example class TestClass { private: int cc; public: TestClass(int i) : cc(i) {}; }; TestClass cc(5); int* pp = (int*)&cc; *pp = 70; // private member has been modified I tested the above code and indeed the private data has been modified. Is there any explanation of why this is allowed to happen or this just an oversight in the language? It seems to directly undermine the use of private data members. Because, as Bjarne puts it, C++ is designed to

What is the benefit of private name mangling in Python?

做~自己de王妃 提交于 2019-11-27 12:13:38
Python provides private name mangling for class methods and attributes. Are there any concrete cases where this feature is required, or is it just a carry over from Java and C++? Please describe a use case where Python name mangling should be used, if any? Also, I'm not interested in the case where the author is merely trying to prevent accidental external attribute access. I believe this use case is not aligned with the Python programming model. It's partly to prevent accidental internal attribute access. Here's an example: In your code, which is a library: class YourClass: def __init__(self)

Which is better between a readonly modifier and a private setter?

。_饼干妹妹 提交于 2019-11-27 11:25:46
I've been working on creating a class and suddenly a thought came to my mind of what is the difference between the two codes: public readonly string ProductLocation; AND public string ProductLocation { get; private set; } Can you guys give me idea when to use the following better. thanks. Bruno Reis The first one is a read-only field, while the second one gets compiled as a pair of methods (and all reads of the property ProductLocation gets compiled into calls to the corresponding get method and writes to it gets compiled into calls to the set method; internally, these methods will read from /

Hiding members in a C struct

自古美人都是妖i 提交于 2019-11-27 06:29:06
I've been reading about OOP in C but I never liked how you can't have private data members like you can in C++. But then it came to my mind that you could create 2 structures. One is defined in the header file and the other is defined in the source file. // ========================================= // in somestruct.h typedef struct { int _public_member; } SomeStruct; // ========================================= // in somestruct.c #include "somestruct.h" typedef struct { int _public_member; int _private_member; } SomeStructSource; SomeStruct *SomeStruct_Create() { SomeStructSource *p =

Purpose of private members in a class

送分小仙女□ 提交于 2019-11-27 05:39:39
问题 What are the purposes of having private/protected members of a class/structure in object-oriented programming? What's the harm in having all members be public? 回答1: Encapsulation. I.e. hiding the implementation of your class data. This allows you to change it later, without breaking all client code. E.g. if you have class MyClass { public int foo; } your clients may write code like MyClass bar = new MyClass(); bar.foo++; now if you realize that foo should actually be a double rather than int,

Why is it allowed to access Java private fields via reflection?

偶尔善良 提交于 2019-11-27 05:27:33
问题 Consider this example : import java.lang.reflect.Field; public class Test { public static void main(String[] args) { C c = new C(); try { Field f = C.class.getDeclaredField("a"); f.setAccessible(true); Integer i = (Integer)f.get(c); System.out.println(i); } catch (Exception e) {} } } class C { private Integer a =6; } It seems illogical that you are allowed to access private fields of classes with reflection. Why is such a functionality available? Isn't it "dangerous" to allow such access? 回答1

Why and how does C# allow accessing private variables outside the class itself when it's within the same containing class?

坚强是说给别人听的谎言 提交于 2019-11-27 04:59:59
I don't know if the question is descriptive enough but why and how does this behaviour exist?: public class Layer { public string Name { get; set; } private IEnumerable<Layer> children; public IEnumerable<Layer> Children { get { return this.children.Where ( c => c.Name != null ).Select ( c => c ); } set { this.children = value; } } public Layer ( ) { this.children = new List<Layer> ( ); // Fine Layer layer = new Layer ( ); layer.children = new List<Layer> ( ); // Isn't .children private from the outside? } } I can access layer.Children anywhere, that's fine, but how can I access layer.children

With a private modifier, why can the member in other objects be accessed directly?

亡梦爱人 提交于 2019-11-27 03:54:09
I have the following code: class A { private: int x; public: A() { x = 90; } A(A a1, A a2) { a1.x = 10; a2.x = 20; } int getX() { return this->x; } }; I know that code might be weird but I don't understand why a1 and a2 can access private data member x ? Good question. The point is that protection in C++ is class level , not object level. So a method being invoked on one object can access private members of any other instance of the same class. This makes sense if you see the role of protection being to allow encapsulation to ensure that the writer of a class can build a cohesive class, and

Accessing private members [closed]

夙愿已清 提交于 2019-11-26 22:25:48
Is it appropriate to access a class' private members by casting it to a void pointer and then to a struct? I don't think I have permissions to modify the class that contains the data members that I need to access. I don't want to take a risk accessing the data members in an indirect way if it is not ethical. EDIT: Had to edit this further... I am pretty sure the class wouldn't be modified, so it's ok to that extent... my only concern is, if the person who coded that class gets to know of this, it might not go down well with him :(. "Never say never". I'm sure somewhere in the universe, there's

a way in c++ to hide a specific function

社会主义新天地 提交于 2019-11-26 21:09:10
问题 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 回答1: The using keyword can be used to change visibility struct A { void method1(); }; struct B: public A { void method2(); private: using A::method1; }; 回答2: 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 &