The following code compiles on a wide range of gcc and clang versions - when compiled and run with gcc 5.3.1, it prints
A()
then aborts with a pure virtual call error.
#include <stdio.h>
class A
{
public:
A() {
printf("A()\n");
}
virtual void b() const = 0;
};
int main()
{
const A& a{};
a.b();
return 0;
}
I realise binding a reference to a temporary is not ideal (though I think this case is covered by some sort of lifetime extension) - but it also works when trying to call a method that takes a const reference like:
Foo({});
For convenience here's an example of it compiling with clang 3.2: Compiler Explorer
Abstract& a = b;
, butb
was a previously constructed object of a concrete class. So we can make a new reference of typeAbstract&
, but I wouldn't have called that constructing an object. I feel like I'm missing something here; probably just misunderstood what you wrote. :-) – GoosestepA
? – Renowned