encapsulation

Can I access private members from outside the class without using friends?

为君一笑 提交于 2019-11-26 03:55:00
Disclaimer Yes, I am fully aware that what I am asking about is totally stupid and that anyone who would wish to try such a thing in production code should be fired and/or shot. I'm mainly looking to see if can be done. Now that that's out of the way, is there any way to access private class members in C++ from outside the class? For example, is there any way to do this with pointer offsets? (Naive and otherwise non-production-ready techniques welcome) Update As noted in the comments, I asked this question because I wanted to write a blog post on over-encapsulation (and how it affects TDD). I

How do I return a reference to something inside a RefCell without breaking encapsulation?

谁说胖子不能爱 提交于 2019-11-26 02:37:57
问题 I have a struct that has inner mutability. use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec<i32>, } struct Foo { //although not used in this particular snippet, //the motivating problem uses interior mutability //via RefCell. interior: RefCell<MutableInterior>, } impl Foo { pub fn get_items(&self) -> &Vec<i32> { &self.interior.borrow().vec } } fn main() { let f = Foo { interior: RefCell::new(MutableInterior { vec: Vec::new(), hide_me: 2, }), }; let borrowed_f = &f; let

Should I return a Collection or a Stream?

荒凉一梦 提交于 2019-11-26 02:27:35
问题 Suppose I have a method that returns a read-only view into a member list: class Team { private List < Player > players = new ArrayList < > (); // ... public List < Player > getPlayers() { return Collections.unmodifiableList(players); } } Further suppose that all the client does is iterate over the list once, immediately. Maybe to put the players into a JList or something. The client does not store a reference to the list for later inspection! Given this common scenario, should I return a

Good way to encapsulate Integer.parseInt()

旧街凉风 提交于 2019-11-26 01:53:25
问题 I have a project in which we often use Integer.parseInt() to convert a String to an int. When something goes wrong (for example, the String is not a number but the letter a , or whatever) this method will throw an exception. However, if I have to handle exceptions in my code everywhere, this starts to look very ugly very quickly. I would like to put this in a method, however, I have no clue how to return a clean value in order to show that the conversion went wrong. In C++ I could have

Can I access private members from outside the class without using friends?

久未见 提交于 2019-11-26 01:52:31
问题 Disclaimer Yes, I am fully aware that what I am asking about is totally stupid and that anyone who would wish to try such a thing in production code should be fired and/or shot. I\'m mainly looking to see if can be done. Now that that\'s out of the way, is there any way to access private class members in C++ from outside the class? For example, is there any way to do this with pointer offsets? (Naive and otherwise non-production-ready techniques welcome) Update As noted in the comments, I

Good way to encapsulate Integer.parseInt()

大城市里の小女人 提交于 2019-11-26 01:17:04
问题 I have a project in which we often use Integer.parseInt() to convert a String to an int. When something goes wrong (for example, the String is not a number but the letter a , or whatever) this method will throw an exception. However, if I have to handle exceptions in my code everywhere, this starts to look very ugly very quickly. I would like to put this in a method, however, I have no clue how to return a clean value in order to show that the conversion went wrong. In C++ I could have

Abstraction VS Information Hiding VS Encapsulation

烂漫一生 提交于 2019-11-26 00:26:30
问题 Can you tell me what is the difference between abstraction and information hiding in software development? I am confused. Abstraction hides detail implementation and information hiding abstracts whole details of something. Update: I found a good answer for these three concepts. See the separate answer below for several citations taken from there. 回答1: Go to the source! Grady Booch says (in Object Oriented Analysis and Design, page 49, second edition): Abstraction and encapsulation are

When should you use &#39;friend&#39; in C++?

≡放荡痞女 提交于 2019-11-26 00:18:48
问题 I have been reading through the C++ FAQ and was curious about the friend declaration. I personally have never used it, however I am interested in exploring the language. What is a good example of using friend ? Reading the FAQ a bit longer I like the idea of the << >> operator overloading and adding as a friend of those classes. However I am not sure how this doesn\'t break encapsulation. When can these exceptions stay within the strictness that is OOP? 回答1: Firstly (IMO) don't listen to

Difference between private, public, and protected inheritance

谁都会走 提交于 2019-11-25 23:57:12
问题 What is the difference between public , private , and protected inheritance in C++? All of the questions I\'ve found on SO deal with specific cases. 回答1: To answer that question, I'd like to describe member's accessors first in my own words. If you already know this, skip to the heading "next:". There are three accessors that I'm aware of: public , protected and private . Let: class Base { public: int publicMember; protected: int protectedMember; private: int privateMember; }; Everything that

Difference between abstraction and encapsulation?

青春壹個敷衍的年華 提交于 2019-11-25 23:48:04
问题 What is the precise difference between encapsulation and abstraction? 回答1: Most answers here focus on OOP but encapsulation begins much earlier: Every function is an encapsulation ; in pseudocode: point x = { 1, 4 } point y = { 23, 42 } numeric d = distance(x, y) Here, distance encapsulates the calculation of the (Euclidean) distance between two points in a plane: it hides implementation details. This is encapsulation, pure and simple. Abstraction is the process of generalisation : taking a