How to Access Private Data Members Outside the Class Without Making "Friend"S

How to access private data members outside the class without making friends?

You can't. That member is private, it's not visible outside the class. That's the whole point of the public/protected/private modifiers.

(You could probably use dirty pointer tricks though, but my guess is that you'd enter undefined behavior territory pretty fast.)

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

If the class contains any template member functions you can specialize that member function to suit your needs. Even if the original developer didn't think of it.

safe.h

class safe
{
int money;

public:
safe()
: money(1000000)
{
}

template <typename T>
void backdoor()
{
// Do some stuff.
}
};

main.cpp:

#include <safe.h>
#include <iostream>

class key;

template <>
void safe::backdoor<key>()
{
// My specialization.
money -= 100000;
std::cout << money << "\n";
}

int main()
{
safe s;
s.backdoor<key>();
s.backdoor<key>();
}

Output:

900000
800000

accessing private members without using friend class

  1. (with pointers) why this happen?

I don't understand this one, so I will skip it.


  1. is such thing possible in other object oriented languages?

Consider python. In python making something private is explicitly only an agreement between the author and the user, but nothing prevents a user from accessing private members. Though, they should not. C++ isn't that explicit about saying "if you want you can access private members", but still it is possible with some effort. Nevertheless you should not. C++ does not prevent you from shooting yourself in your foot and accessing private members is one way of doing that. It isn't the case in your example, but typically accessing private members directly will break the object beyond repair.


  1. does this mean access modifiers are useless?

I'll repeat my comment: Is a traffic light useless? I mean when it is red I can still cross the street. Access specifiers are not there to prevent you from doing something wrong by all means, they are to help you to avoid doing something wrong (and if you try hard you can still do something wrong).


  1. and is there anything we can do to prevent such thing to happen?

Declaring a member as private is enough to signal that a user should not access the member directly by any means. If someone wants to break that agreement then they can do it. You cannot prevent a user from doing something wrong. If they want to break your class they can do so. However, it is not your responsibility to guarantee that something broken still works as expected. If a user bypasses access specifiers then they broke the agreement between them and you. Consider you buy a laptop and throw it out of the window from 42th floor. Will you complain to the manufacturer that afterwards the laptop is not working properly anymore? I guess no, instead you will understand that you made something wrong with using your laptop.

PS: Your last two examples are undefined behavior. reinterpret_cast is not a way to cast between arbitrary types magically. The set of allowed casts and what you can do with the results is in fact rather limited (see here). Also a c-style cast enables you do to casts that can be very wrong, without your compiler complaining about it. Thats why they should be avoided in favor of the proper c++ casts (static_cast et al).

How to access private class members with out being in the in class

Usually, the member functions and the friends of the class can access the private members of a class, via an instance of the class.

Therefore, in your case, make the delete_Data function as friend function.

class data 
{
private:
std::string name;
int id;
public:
// ...
friend void delete_Data(data& obj);
};

void delete_Data(data& obj)
{
obj.name = "";
obj.id = 0;
}

Now in the main()

data one;
delete_Data(one);

Also note that, you need to pass an instance of the data class to access the members of the class.


Side notes:

  • There are other tricky ways, by which we can also
    access the member. However, the whole point of the
    public/protected/private modifiers, is not to do that.

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

  • Also see
    Why is "using namespace std;" considered bad practice?

Why can we access private data members class using pointers, without using members friend function other members in the class?

Actually the behaviour of using the pointer obtained by the statement

int* ptr = (int*)&t; 

is undefined. (Technically this is a strict aliasing violation).

C++ is littered with undefined constructs - which is one thing that makes the language fiendishly difficult to master. Given you're just setting out leaning C++ (there's no substitute for a good book by the way), avoid all casts until you know what you're doing.



Related Topics



Leave a reply



Submit