0

X works but Y does not. Whats the difference between X and Y? And passing them as base reference to foo?

class Base { };
class Derived : Base{};

void foo(Base& p) {}

int main() {
    Derived x = Derived();
    Derived y();

    foo((Base&)x);
    foo((Base&)y);
}
  • 1
    That's called [most vexing parse](https://en.wikipedia.org/wiki/Most_vexing_parse): https://stackoverflow.com/questions/14077608/what-is-the-purpose-of-the-most-vexing-parse –  Apr 15 '21 at 19:48
  • 1
    Derived *is not a* Base, because of `private` inheritance. Change `Derived y();` to `Derived y{};` or just `Derived y;`. – Eljay Apr 15 '21 at 19:49
  • `Derived y();` is declaring a **function** named `y` that takes no parameters and returns a `Derived`. It is NOT declaring a **variable** named `y` of type `Derived` that is default-constructed, as you are expecting. Use `Derived y;` or `Derived y{};` for that variable instead. – Remy Lebeau Apr 15 '21 at 19:51
  • Ok thank you I understand now... – coleemersonsmith Apr 15 '21 at 19:54

0 Answers0