例如在 jQuery 中,您可以封装函数以在一行中对一个对象执行许多操作。
$('div').parent().find('a').is('.class').css('color', 'red');
我不知道如何它在内部是如何工作的。如何用 C++ 编写具有类似行为的类?
最佳答案
这称为方法链接,您可以在 C++ 中实现相同的效果,具体取决于您的返回类型。
struct A
{
A& foo()
{
return *this;
}
A& goo()
{
return *this;
}
};
A a;
a.foo().goo().foo();
这只是一个简单的例子。
关于c++ - 在 C++ 中提供 jQuery 风格的可封装函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12973502/