C ++: address of links?

My knowledge of C ++ arcana is a little rude. Let's say I have the following classes:

struct Bar {
  int x;
};

class Foo {
  Bar& bar;
public:
  Bar* getRealAddress() { return &bar; }
  Foo(Bar& _bar) : bar(_bar) {}
};

Bar bar1;
Foo foo1(bar1);

Will it foo1.getRealAddress()return the same value as &bar1?

+3
source share
2 answers

Will it foo1.getRealAddress()return the same value as &bar1?

Yes.

Basically, the link is the original value in all but the name. It is a nickname.

+11
source

Yes, the addresses will be the same as the same instance Bar.

+2
source

Source: https://habr.com/ru/post/1789832/


All Articles