I wonder why this program does not compile (the same behavior in msvc, gcc and clang):
#include <iostream>
using namespace std;
struct Action
{
virtual void action()
{
cout << "Action::action()\n";
}
};
struct ActionDecorator : Action
{
ActionDecorator(const ActionDecorator&) = delete;
ActionDecorator(Action & action) : origAction(action)
{
}
void action() override
{
decoration();
origAction.action();
}
private:
void decoration()
{
cout << "ActionDecorator::decoration()\n";
}
Action & origAction;
};
int main()
{
Action action;
ActionDecorator actionDecorator(action);
ActionDecorator actionDecorator2(actionDecorator);
actionDecorator2.action();
}
According to my expectation, the remote copy instance should allow another ActionDecorator instance to create an ActionDecorator, since it is a polymorphic type of Action. Instead, I must explicitly specify an ActionDecorator instance for Action & since the compiler complains about trying to reference a remote copy instance. Is there any standard rule to explain this behavior?
source
share