Retaining *this in method chaining

Discussion in 'C++' started by Ashwin Nanjappa, Jan 19, 2007.

  1. 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
    Ashwin Nanjappa, Jan 19, 2007
    #1
    1. Advertising

  2. Ashwin Nanjappa

    Ian Collins Guest

    Ashwin Nanjappa wrote:
    > Hi folks,
    >
    > I've got a class setup as shown below:
    >
    > class A
    > {
    > public:
    > A& call()

    virtual A& call()

    > { return *this; }
    >
    > void init()
    > { }
    > };
    >
    > class B : public A
    > {
    > public:


    B& call()
    { return *this; }
    > void init()
    > { }
    > };
    >


    --
    Ian Collins.
    Ian Collins, Jan 19, 2007
    #2
    1. Advertising

  3. Ashwin Nanjappa

    Rolf Magnus Guest

    Ashwin Nanjappa wrote:

    > 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.


    B doesn't even have its own implementation.

    > 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?


    Yes. Make A::call() virtual. If your text book doesn't describe how to use
    polymorphism in C++, throw it away, because that's a basic language
    feature.
    Rolf Magnus, Jan 19, 2007
    #3
  4. On Jan 19, 7:01 pm, Rolf Magnus <> wrote:
    [...]
    > Yes. Make A::call() virtual. If your text book doesn't describe how to use
    > polymorphism in C++, throw it away, because that's a basic language
    > feature.


    Thanks, this is exactly what I was looking for.

    Regards,
    ~ash
    Ashwin Nanjappa, Jan 31, 2007
    #4
  5. On Jan 19, 1:06 pm, Ian Collins <> wrote:
    > Ashwin Nanjappa wrote:
    > > Hi folks,

    >
    > > I've got a class setup as shown below:

    >
    > > class A
    > > {
    > > public:
    > > A& call()

    >
    > virtual A& call()
    >
    > > { return *this; }

    >
    > > void init()
    > > { }
    > > };

    >
    > > class B : public A
    > > {
    > > public:

    >
    > B& call()
    > { return *this; }
    >
    > > void init()
    > > { }
    > > };


    Thanks Ian. In my case, making init() virtual did the trick.

    Regards,
    ~ash
    Ashwin Nanjappa, Jan 31, 2007
    #5
  6. Ashwin Nanjappa

    Rossonera Guest

    Hi,
    I think there are two ways to resolve your problem.
    1. add B& call() {} member function.
    2. declare void init() as virtual in class A.
    both of them can resolve your problem.
    Rossonera, Jan 31, 2007
    #6
  7. On Jan 31, 10:08 pm, "Rossonera" <> wrote:
    > Hi,
    > I think there are two ways to resolve your problem.
    > 1. add B& call() {} member function.
    > 2. declare void init() as virtual in class A.
    > both of them can resolve your problem.


    Yes, this is why I was a bit confused. Solution 2 works for me. This
    is because this particular class(es) have a lot of methods that can be
    chained but only one init call.

    ~ash
    Ashwin Nanjappa, Feb 1, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    30
    Views:
    7,853
    Raymond DeCampo
    Sep 6, 2005
  2. Dmytro Sheyko
    Replies:
    3
    Views:
    1,981
    Dmytro Sheyko
    Jan 19, 2006
  3. Makoto Kuwata
    Replies:
    0
    Views:
    230
    Makoto Kuwata
    Feb 16, 2011
  4. aartist

    Method Chaining Issues

    aartist, Jun 1, 2005, in forum: Ruby
    Replies:
    25
    Views:
    278
    Hal Fulton
    Jun 3, 2005
  5. snacktime

    method chaining via symbols

    snacktime, Sep 5, 2008, in forum: Ruby
    Replies:
    2
    Views:
    111
    James Coglan
    Sep 5, 2008
Loading...

Share This Page