1

I had a homework which i was asked how to access private members of a class and modify them in c++.I searched about it and i found out that we can do it with typecast and pointers which i know it's an undefined behavior and it should never be used.My question is: Is it possible to do such thing in other object oriented languages like java or python?

Fargol_sh
  • 21
  • 1
  • 4
  • 2
    Did you read a [C++ programming](http://stroustrup.com/programming.html) book or [C++ tutorial](http://www.cplusplus.com/) or [C++ reference](https://en.cppreference.com/w/cpp) website? If you did, [edit](https://stackoverflow.com/posts/62230342/edit) your question to add some [mre] in your question please. StackOverflow is *not* a *do my homework* website. The answer to your question is: **yes** – Basile Starynkevitch Jun 06 '20 at 10:29
  • Read also more [about Java](https://en.wikipedia.org/wiki/Java_(programming_language)) and about [Python](https://python.org/). Feel free to contact me by email to `basile@starynkevitch.net`, but mention there the URL of your question – Basile Starynkevitch Jun 06 '20 at 10:33
  • 1
    why are you getting taught to break the concept of privacy in the programming...? – ΦXocę 웃 Пepeúpa ツ Jun 06 '20 at 10:38
  • @BasileStarynkevitch yes i'm learning c++ and my question is not about it,i'm asking about other object oriented languages,i searched a lot and i didn't find out any thing useful related to my question. – Fargol_sh Jun 06 '20 at 10:42
  • What other object programming languages do you have in mind? Did you read about the [ObjVLisp object model](https://pdfs.semanticscholar.org/051f/852ac2883a694c51151c88490603867f5ffb.pdf) ? Or about the [Common Lisp Object System](https://en.wikipedia.org/wiki/Common_Lisp_Object_System) ? See [Common Lisp HyperSpec](http://www.lispworks.com/documentation/HyperSpec/Front/) – Basile Starynkevitch Jun 06 '20 at 10:43
  • @ΦXocę웃Пepeúpaツ it's just a challenge we're asked to search about it.I know it's not recommended to use pointers to access private members directly. – Fargol_sh Jun 06 '20 at 10:46
  • [Python](http://python.org) or C++ (with [GCC](http://gcc.gnu.org/) ...) or Common Lisp (with [SBCL](http://sbcl.org/)...) or even [Java](https://en.wikipedia.org/wiki/Java_(programming_language)) or [Go](http://go-lang.org/) have **[open source](https://en.wikipedia.org/wiki/Open_source) implementations. Download them** and study their source code. – Basile Starynkevitch Jun 06 '20 at 10:47
  • you could create getter and setter functions for the private members. that is not exactly orthodox but it is possible. you could create a getter that returns a reference to the private member. – Cristi Jun 06 '20 at 12:45

2 Answers2

1

The C++ programming language has a friend specifier. Friend function can see its friend class' private members. But more young languages don't include this mechanism. Because the mechanism isn't correct for object oriented programming paradigm(for encapsulation).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 2
    Wrong wording. in C++, [`friend` is a specifier](https://en.cppreference.com/w/cpp/language/friend) not a function (like [`assign` in `std::string`](https://en.cppreference.com/w/cpp/string/basic_string/assign) is) – Basile Starynkevitch Jun 06 '20 at 10:53
  • Re: "the mechanism isn't correct for object oriented programming paradigm (for encapsulation)" -- that's simply not true. `friend` declarations respect encapsulation; from the class definition you can find everything that can access private members: member functions, friend functions, and member functions of friend classes. That's encapsulation. – Pete Becker Jun 06 '20 at 12:33
1

In C++ you can write a member function that can access and modify private members and make such function public. This is a common way for OOP. However, of course, different languages including C++ may provide hax to modify protected members in another way.

class T {
public:
  int get() const {
    return _member;
  } 
  void set(int member) {
    _member = member;
  } 
protected:
  int _member;

};

List of well-known hacks

You can easily access any member in Python. Just dir whatever you what to hack. Private members in Python.

You can hack C++ members with template http://bloglitb.blogspot.com/2011/12/access-to-private-members-safer.html . It is much safer than use pointers.

You can access private members via reflection in Java https://docs.oracle.com/javase/tutorial/reflect/.

Generally, any language that lets you debug the code should also reveal somehow protected variables.

Askold Ilvento
  • 1,405
  • 1
  • 17
  • 20