I have some code where IInterface is an abstract class.
I was writing this
IInterface &q = InterfaceImpl();
and compiled it in Visual Studio 2008 and it worked fine. Then I ported it to a gcc project and suddenly I got this error:
error: invalid initialization of non-const reference of type 'IInterface&' from a temporary of type 'InterfaceImpl'
When trying to find out what is wrong I found this thread error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’ and at least understand now what is wrong. So changing it to this:
InterfaceImpl i = InterfaceImpl();
IInterface &q = i;
made it compile:
However, The lifetime of these two objects are the same, so I don't really understand why gcc can not create a temporary object as apparently MS did. I assume that this is defined by the standard? When I look at the link above, I can understand why it makes no sense with something like a base type, but why does it need to get an error in case of an object?
I also tried it like this, but then I get the error that IInterface is an abstract class and can not be instantiated.
IInterface i = InterfaceImpl();
i
should be initialized, not instantiated. Does this mean I would need a copy constructor or assignment operator in InterfaceImpl to make this work or why do I get this error?