简体   繁体   中英

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM