I am trying to create an object that lives in a struct and pass that struct to another object while pointing to the same address. Here is a minimal example of what I am trying to do:
#include <iostream>
class A {
};
struct S {
A* a;
S(A* c) { a = c; }
};
class B {
public:
B(S* s){ s_ = s; }
S* get_s() {return s_;}
private:
S* s_;
};
int main() {
A* a = new A();
S* s = new S(a);
B b = B(s);
std::cout << "a in A = " << &a << "\n";
std::cout << "a in S = " << &s->a << "\n";
std::cout << "a in B = " << b.get_s() << "\n";
//output:
// a in A = 0x7ffe81376918
// a in S = 0x2563e90
// a in B = 0x2563e90
}
I am expecting for all a
to point to the same address.
Aucun commentaire:
Enregistrer un commentaire