Jason S
Jason S

Reputation: 189626

C++: address of references?

My knowledge of C++ arcana is a bit rough. 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 foo1.getRealAddress() return the same value as &bar1?

Upvotes: 3

Views: 252

Answers (2)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84159

Yes, addresses will be the same since it's the same Bar instance.

Upvotes: 2

sbi
sbi

Reputation: 224049

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

Yes.

Basically, a reference is the original value in all but name. It's an alias.

Upvotes: 11

Related Questions