How do I access a private member in a C++ library without modifying the library?

How do I access a private member in a C++ library without modifying the library?

>inb4 C++ priests have a sperg attack

Other urls found in this thread:

gist.github.com/aras-p/6224951
twitter.com/SFWRedditImages

you can't, a member marked private can't be accessed by subclasses and if it doesn't have any friend classes specified you're SOL

just modify the library

you might be able to do some janky pointer arithmetic if you know enough about how it works, but at that point, just modify the fucking library

You've heard of introspection, right? Well, C++ has this feature called retrospection. You look back at what you're doing and go "Shit, I've been going about this all wrong" and go back to the drawing board.

Just use Python lol. I don't get why you low level fags resort to this autistic language for such simple tasks.

What if the library doesn't allow redistribution of modifications?

What if feeding my family requires me to implement a feature, which requires me to distribute software that accesses a private member?

I don't understand this can't-do philosophy of C++. We can go to the moon, build megastructures, but we can't get around a constraint in a C++ compiler.

#define private public
#define protected public
#include
I shit you not

>it actually fucking works

C++ modules need to be standard to stop shit like this

lol

1) This is not ABI compatable (although it may be) and violates the One-Declaration-Rule, so it is undefined behavior.
2) I would add to that
#define class struct
3) To solve (1), I would use -Dprivate=public -Dprotected=private -Dclass=struct when compiling the library.

Just when you think you've seen it all..

How about
#define == !=
#define < >


Good lord...

Here's a subtle one
#define true -1

This is now a bad #defines thread.

Try using 'friend' declaration. It would be way better solution than the defines shit that people are suggesting.

This is the funniest shit I've seen today

#define if if (rand() < 1000) if

You've missed the point. If you were able to modify the class in the library, this wouldn't be a problem.

10/10
would run in production

And people say PHP is a bad language.

ayy lmao

Write an identical struct with no private members, and cast a pointer to the original class to a pointer to the struct type.

class Foo {
int a; // Private by default
public:
int b;
};

struct Bar {
int a; // Both public by default
int b;
};

void manip_foo(Foo& foo, int val) {
Bar *bar = reinterpret_cast(&foo);
bar->a = val;
}

There are not constraints in C++ compilers. You can skirt the type system any time you want. You just really, really shouldn't.

This is the same as the #define private public trick. It may break ABI compatibility because of the One-Declaration-Rule.

Of course, as anyone knows, rules are meant to be broken, but only once you understand the consequences.

This doesn't make it bad...

heh

>It may break ABI compatibility because of the One-Declaration-Rule.

No it won't, there's no redeclaration going on here, you just declare a similar but different class and then reinterpret_cast to it.

Like this

// In library

class SomeClass
{
public:
int a;
int b;
private:
int c;
};

// In your code

int main()
{
SomeClass obj;

int* pObj = (int*)obj; // Pretend it's a an array of ints

int offset = 2; // Be very careful, this offset may not be what you expect because of member alignment (look up "the lost art of C struct packing" if you're confused)
int c = pObj[offset]; // Access the offset int
}

Oops, should be:

int* pObject = (int*)&obj;

Who cares if it works? There's a lot of ways to access the private implementation. It's not like you're "hacking", all you're doing is fucking yourself by doing it and making your program fragile as fuck. There's not really any reason to make it impossible to get at, especially since once in a million years there might be a good reason to do it.

If the library developer really cared if their users are going to be retards, they could just pull a Cheshire cat and then your only option would be reverse engineering and fucking around with pointers to mess with the class internals.

my c++ teacher showed us this as a example.
he wanted to let us know what "private" does, and what it not does

I believe this is still false. The C++ compiler is not required by the standard to align public and private fields in the same way, or align members in their vtables if they are virtual. I would assume most compiler do though, so in practice it will usually work.

...

Jesus christ, you fucking CS freshmen. You're not outsmarting shit here. Access control is purely a compiler enforced scheme to stop Pajeets like you from breaking their own shit. There's no runtime enforcement. Even managed languages with runtime enforcement have methods of bypassing access control because IT'S ASSUMED THAT IF THE FUCKING PROGRAMMER IS EXPLICITLY DEMANDING ACCESS TO THE PRIVATE IMPLEMENTATION THEY PROBABLY HAVE A GOOD REASON.

#define if(x) if((x) && rand() % 1000)

The compiler makes no guarantee in the first case that `a` comes before `b`. Learn the standard. That's undefined behavior.

Finally someone who gets it

The standard may say one thing, but the language is so prohibitively difficult to write compilers for that there are very few up to date ones, and none of them to my knowledge will re-order or misalign private members.

And this is what I do in practice. But it's a bit unnerving to leave something as undefined because it may bite you later when GCC 11.0 decides to add some optimization on private members that breaks this assumption.

Whta
hte
fukc

Why do you think it's private, dumbfuck?

Because the developer of the library thinks that because the internal workings may change in the future, they should be outright restricted from all tampering.

THE ABSOLUTE STATE OF C++

just make everything a global variable you cuck

...

this

#define private public
#define class struct

Get address of function and Hackett dackery doo it.

I don't respect oop practices or oop practicers.
A private member will be stored in memory somewhere. Depends on packing. So the 'safest' way is to replicate the class in your own code (copy paste if you have the source). Grab the offset from the object head for each of the private variables you wish to access and offset yourself from the instance with the member you're looking to access to access/modify.

If you don't have any source and you're dealing with a pre-compiled library you could construct the object with known values that set the private member to something unique and known and then you walk through the memory of the class looking for the unique value. How many bytes you can jump at a time depends on platform. But when you find it you have your offset. Save it, use it later when you wish to poke into the object.

Of course the only right way to do this is to abandon the library. But sometimes you have to make a deal with the enemy. I'm not judging.

Didn't even think of that. Not a bad idea.

Doing so would break the ABI. Libraries would need to be recompiled for the new GCC.

can someone spoonfeed a brainlet like me why this is funny

Define a friend/getter function in public

If there's a private member storing data chances are there's an interface to read and write it. Have you read the documentation?

If there really isn't an interface provided just modify the library.

My fucking sides

Do you see why any of these would be funny?

gist.github.com/aras-p/6224951

The power of text replacement macros

How do you know they don't have good reason, you fucking weeb? Maybe the people who wrote the library were pajeets.

...

Yes you can. Sepples is trash when it comes to encapsulation.

Use pointer forging.

kek

Reflection

checked and keked