non-const reference to temporary derived from pure virtual Base
Hi Ng,
besides that this style may be asking for trouble i'm faced with a
unexpected gcc behaviour. Tested with gcc 3.3.3.
On M$ .Net this compiles just fine.
I'm trying to make a non-const reference to a temporary object. The
object is derived from a pure virtual base, so the compiler can't create
a copy of base-type.
Why does this not work with gcc?
To reproduce:
//-----------------------------------
class Base {
public:
Base() {};
virtual void pv() = 0;
virtual ~Base() {};
};
class Derived : public Base {
public:
Derived(int i) {};
virtual void pv() {};
virtual ~Derived() {};
};
class User {
public:
void useBase(Base& b) {};
void useBaseConst(const Base& b) {};
};
int main() {
User u;
Derived d(1);
// Ok with non-temporary !
u.useBase(d);
// not ok!
u.useBase(Derived(1));
//* no matching function for call to `User::useBase(Derived)'
//* candidates are: void User::useBase(Base&)
// seems that the compiler refuses to make a
// non-const reference to a temporary
// but this is ok!
u.useBaseConst(Derived(1));
}
Regards,
Michael