我想知道是否可以自动将方法调用转发给嵌入对象,而不继承.例如:
class embed
{
public:
void embed_method() {return};
};
class container
{
public:
void container_method() {return;}
private:
embed obj;
};
int main()
{
container object;
object.container_method(); // Local method call
object.embed_method(); // 'Forward' call, obviously doesn't work
}
Run Code Online (Sandbox Code Playgroud)
当不可能/不推荐从基类继承时,它可能非常有用.目前,我唯一的选择是手动将embed
类方法重写为container
类,然后embed
从中调用方法container
.即使该过程可以编写脚本,它仍然很烦人,似乎是一个糟糕的解决方案.
c++ ×1