A
Ashwin Nanjappa
Hi folks,
I've got a class setup as shown below:
class A
{
public:
A& call()
{ return *this; }
void init()
{ }
};
class B : public A
{
public:
void init()
{ }
};
B b;
b.call().init();
This calls class A's init instead of that of B. This is because call()
returned object of type A instead of B. Is there any other way to
design these classes so that functions like call() know how to return
their actual object instead of the base object?
Why do I want this crazy setup? I want to retain the ability to do
method chaining (b.call1().call2().call3() ...) But, doing it on
inherited classes complicates matters like this :-(
Rgds,
~ash
I've got a class setup as shown below:
class A
{
public:
A& call()
{ return *this; }
void init()
{ }
};
class B : public A
{
public:
void init()
{ }
};
B b;
b.call().init();
This calls class A's init instead of that of B. This is because call()
returned object of type A instead of B. Is there any other way to
design these classes so that functions like call() know how to return
their actual object instead of the base object?
Why do I want this crazy setup? I want to retain the ability to do
method chaining (b.call1().call2().call3() ...) But, doing it on
inherited classes complicates matters like this :-(
Rgds,
~ash