Hello, I want to make chain-calling like jquery-way in c++. The sample:
$('#obj').getParent().remove();
So, as I understand, each method of the class should return the pointer to himself (this).
Everything is okay until I call base-derived methods. The code:
class Base
{
Base *base1() { return this; }
Base *base2() { return this; }
};
class Derived : Base
{
Derived *derived1() { return this; }
Derived *derived2() { return this; }
};
Derived *obj = new Derived();
obj->derived1()->derived2(); // Everything is okay
obj->derived1()->base1()->derived2(); // Fail at second step
Sure, the base1 returns the pointer for the Base. Are there any ways to make automatic casting?
UPD: Maybe that's possible with macros? Like
#define CORRECT_RETURN (this)
and
Base::base1() {
return CORRECT_RETURN;
}
Something in this way. Or the compiler will not look at such construction?