-3

I know how to access std::string in class S, but how can i get int i in this class? What should i do? I understand i should shift the pointer, but in what way?

#include <iostream>
#include <string>

class S {
    std::string s = "abcd";
    int i = 9;
};


int main() {
    
    S s;

    auto* f = reinterpret_cast<std::string*>(&s);
    
    std::cout << *f;

    return 0;
}

i tried something like

auto* f = reinterpret_cast<char*>(&s); f += sizeof(std::string("abcd");

but it didn't work (i know it even looks silly but i tried >-< )

  • which struct? There's only a class. – Thomas Weller Oct 29 '22 at 20:00
  • You shouldn't be able to. That is the point of `private`. `&S()` is also not valid C++. – user17732522 Oct 29 '22 at 20:00
  • 1
    `&S()` is a non-standard MSVC extension, I believe. `S()` is an rvalue, and you shouldn't be able to take an address of one. Even if it works on your compiler, the temporary `S` object is destroyed immediately, leaving you with a dangling pointer. – HolyBlackCat Oct 29 '22 at 20:03
  • 1
    There is no way to do that without invoking Undefined Behaviour. – Yksisarvinen Oct 29 '22 at 20:04
  • Your attempt looks ok to me. You forgot the closing `)`, and you need to `reinterpret_cast` the resulting pointer to `int *` before dereferencing it. Also note that `sizeof(std::string("..."))` is the same as `sizeof(std::string)`. The value of a class doesn't affect its size, which is fixed at compile time. – HolyBlackCat Oct 29 '22 at 20:06
  • perhaps if you explain what you are trying to achieve a better option will become clear - I am almost certain what you are attempting is not a good idea :p – code_fodder Oct 29 '22 at 20:08
  • (a) the code does not compile - live - https://godbolt.org/z/fx19n7oTc ; (b) fixing the compilation error ( https://godbolt.org/z/Y3sohvsGP ) result in Undefined Behaviour as `S` and `std::string` are not related types. See [Type aliasing](https://en.cppreference.com/w/cpp/language/reinterpret_cast#Type_aliasing) – Richard Critten Oct 29 '22 at 20:28
  • @Yksisarvinen there are a lot of ways to legally access private members of class/struct, the access specifier only applies to the name (not the actual peace of memory). – Dmitry Oct 29 '22 at 20:36
  • 1
    @RichardCritten it's valid code, as long as `S` is standard-layout (but `std::string` may not guarantee to be one (then also `S`)). – apple apple Oct 29 '22 at 20:42
  • yes, &S() is not valid, there should be S s; &s – Fluffle Puff Oct 31 '22 at 16:14
  • I know i shouldn't be able to access private members but bruuuuh, it's C++. If i want, i will be able to :) – Fluffle Puff Oct 31 '22 at 16:15
  • @Dmitry hi! could you pls elaborate a little bit more on ways of legally accessing private data? – Fluffle Puff Oct 31 '22 at 16:33

1 Answers1

-4

So, the answer is:

S s;

auto* f = reinterpret_cast<std::string*>(&s);

int* ii = reinterpret_cast<int*>(f + 1);

std::cout << *ii;

Thanks everyone for attention, hope this will help someone ;)