Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How references are handled in memory

Tags:

c++

class B {
};

class A {
    A(B& b):_b(b)  
    B& _b;
};

int main() {
    B b;
    A a(b);
    char* x = reinterpret_cast<char*>(&a);
}                               

I'm creating a hash function based on the byte values of the objects. I want to know wether the bytes of object a will hold b or will they hold a reference (pointer)?

like image 683
mkmostafa Avatar asked Jan 31 '26 06:01

mkmostafa


1 Answers

As you declared A::_b as a reference, it will "hold" a reference. Thus the object a does not contain the data of b if you examine a byte-wise.


By the way you forgot to use the address-of operator in your cast.

like image 104
Some programmer dude Avatar answered Feb 03 '26 00:02

Some programmer dude