The following code doesn't compile:
#include<iostream>
class base
{
public:
virtual ~base() = default;
virtual void func(const int &i) = 0;
};
class derived : public base
{
void func(int &i)
{
}
};
int main(int argc, char const *argv[])
{
std::unique_ptr<base> ptr{new derived};
return 0;
}
giving the error
test.cpp:28:34: error: allocating an object of abstract class type 'derived'
However, if I remove the pass &
in the parameters for both base
and derived
, the code compiles, but the function signatures are still different. Why does the former not compile, but the latter does? I don't understand how the pass by reference affects the compilation in this case.