我想知道是否可以自动将方法调用转发给嵌入对象,而不继承.例如:
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.即使该过程可以编写脚本,它仍然很烦人,似乎是一个糟糕的解决方案.
class embed
{
public:
void embed_method(){return};
};
class container
{
public:
void container_method(){return;}
embed GetObj(){return obj;}
private:
embed obj;
};
int main()
{
container object;
object.container_method(); // Local method call
object.GetObj().embed_method();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1554 次 |
| 最近记录: |