encapsulation

Dosen't Reflection API break the very purpose of Data encapsulation?

隐身守侯 提交于 2019-11-30 03:40:09
问题 Very recently I came across the Reflection API and to my surprise we can access and even alter the private variables.I tried the following code import java.lang.reflect.Field; public class SomeClass{ private String name = "John"; } public class Test{ public static void main(String args[]) throws Exception { SomeClass myClass = new SomeClass(); Field fs = myClass.getClass().getDeclaredField("name"); fs.setAccessible(true); System.out.println("Variable is " + fs.getName() + " and value is " +

Is there any workaround for making a structure member somehow 'private' in C?

给你一囗甜甜゛ 提交于 2019-11-30 00:30:44
I am developing a simple library in C, for my own + some friends personal use. I am currently having a C structure with some members that should be somehow hidden from the rest of the application, as their use is only internal. Modifying by accident one of this members will probably make the library 'go wild'. Is there any 'workaround' to hide those members so that they can't be accessible ? The usual techique is this: /* foo.h */ typedef struct Foo Foo; Foo *foo_create(...); void foo_bark(Foo* foo, double loudness); /* foo.c */ struct Foo { int private_var; }; You can partially hide data

How can I expose iterators without exposing the container used?

旧街凉风 提交于 2019-11-29 23:02:20
I have been using C# for a while now, and going back to C++ is a headache. I am trying to get some of my practices from C# with me to C++, but I am finding some resistance and I would be glad to accept your help. I would like to expose an iterator for a class like this: template <class T> class MyContainer { public: // Here is the problem: // typedef for MyIterator without exposing std::vector publicly? MyIterator Begin() { return mHiddenContainerImpl.begin(); } MyIterator End() { return mHiddenContainerImpl.end(); } private: std::vector<T> mHiddenContainerImpl; }; Am I trying at something

Linking enum value with localized string resource

依然范特西╮ 提交于 2019-11-29 20:34:48
问题 Related: Get enum from enum attribute I want the most maintainable way of binding an enumeration and it's associated localized string values to something. If I stick the enum and the class in the same file I feel somewhat safe but I have to assume there is a better way. I've also considered having the enum name be the same as the resource string name, but I'm afraid I can't always be here to enforce that. using CR = AcmeCorp.Properties.Resources; public enum SourceFilterOption {

How to use Dependency Injection without breaking encapsulation?

这一生的挚爱 提交于 2019-11-29 20:17:56
How can i perform dependency injection without breaking encapsulation? Using a Dependency Injection example from Wikipedia : public Car { public float getSpeed(); } Note: Other methods and properties (e.g. PushBrake(), PushGas(), SetWheelPosition() ) omitted for clarity This works well; you don't know how my object implements getSpeed - it is " encapsulated ". In reality my object implements getSpeed as: public Car { private m_speed; public float getSpeed( return m_speed; ); } And all is well. Someone constructs my Car object, mashes pedals, the horn, the steering wheel, and the car responds.

How to integrate a library that uses expression templates?

馋奶兔 提交于 2019-11-29 17:19:28
问题 I would like to use the Eigen matrix library as the linear algebra engine in my program. Eigen uses expression templates to implement lazy evaluation and to simplify loops and calculations. For example: #include<Eigen/Core> int main() { int size = 40; // VectorXf is a vector of floats, with dynamic size. Eigen::VectorXf u(size), v(size), w(size), z(size); u = 2*v + w + 0.2*z; } Since Eigen uses expression templates, code like u = 2*v + w + 0.2*z; In the above mentioned sample reduces to a

Is it a bad practice to add elements to List using getter method in java?

断了今生、忘了曾经 提交于 2019-11-29 16:57:09
问题 Suppose I have a private ArrayList or a LinkedList inside a class, that I will never assign new reference to it, or in other words this will never happen: myLinkedList = anotherLinkedList; So that I won't need to use setMyLinkedList(anotherLinkedList) . But! I need to add elements to it, or remove elements from it. Should I write a new kind of setter to only, do the task of adding instead of setting , like myLinkedList.add(someElement) ? Or it is OK to do this by using getter , without

Is there a way to write an equality test for a VBA class with private members without exposing knowledge of the existence of those private members?

五迷三道 提交于 2019-11-29 14:58:09
问题 I do a fair amount of Excel VBA programming, but not a lot of it is object-oriented. Here is something that comes up every now and then that bugs me, and I'm wondering if there's something I'm missing. In VBA, say I have a class C defined with some private members like so: '... Private hidden1_ As Double Private hidden2_ As Double '... If VBA worked like C++ or (most?) other languages that support OOP, I could write a member function to do an equality test between instances of class C like

C# accessing protected member in derived class [duplicate]

浪子不回头ぞ 提交于 2019-11-29 13:37:28
This question already has an answer here: Why can't I access C# protected members except like this? 7 answers I wrote the following code: public class A { protected string Howdy = "Howdy!"; } public class B : A { public void CallHowdy() { A a = new A(); Console.WriteLine(a.Howdy); } } Now, in VS2010 it results in the following compilation error: Cannot access protected member 'A.a' via a qualifier of type 'A'; the qualifier must be of type 'B' (or derived from it). This seems quite illogical to me - why can't I access the protected field of the class instance from a method of the class, which

Why can't the VBA Me keyword access private procedures in its own module?

橙三吉。 提交于 2019-11-29 13:11:00
I just discovered that the Me keyword cannot access private procedures even when they are inside its own class model. Take the following code in Class1: Private Sub Message() Debug.Print "Some private procedure." End Sub Public Sub DoSomething() Me.Message End Sub This code instantiates an instance of the class: Sub TestClass() Dim objClass As New Class1 objClass.DoSomething End Sub Me.Message throws compile error "Method or data member not found." If I change Private Sub Message() to Public the procedure works fine. I can also remove the Me keyword from the DoSomething procedure, but I was