cafekaze
cafekaze

Reputation: 377

Passing Reference to Pointers

I'm passing references to pointers in order to use virtual functions.

Option 1 does what I intended and calls the appropriate derived class virtual function. Option 2 does not, suggesting no reference was passed. Since this does compile, then what does & mean in this context if not pass by reference?

1.
Derived aDerived;
Derived *pDerived = &aDerived;

2.
Derived *pDerived = &Derived();

Upvotes: 0

Views: 143

Answers (1)

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33944

In option 2 you are attempting to take the address of an rvalue. This should fail to compile with:

error: lvalue required as unary ‘&’ operand

Check this live example.

Upvotes: 1

Related Questions