1

I am trying to create a system where one base class has a bunch of functions, then derived classes can inherit it and add more functions. However, all of these functions return the this keyword and I need the return type in derived classes to be a pointer of their own class, not of the base class. What might be the best way of implementing this?

Preferably, the system would work such that functions can be called on a variable one after another in a single line. For example, testObject->SetPositionX(5)->SetPositionY(12)->SetRGB(24, 80, 50); or even testObject->AddChild((new Foo)->SetBorderWidth(5)->SetBorderStyle(BorderStyle::dotted)); The issue, however, is that if a function is called that's defined in the base class instead of the derived class, it will return a pointer of the base class type and remove access to all of the derived class functions.

As a full demonstration,

#include <iostream>

class Base {
    public:
        Base* CommonFunc() {
            std::cout << "Common function part of Base and all derived classes" << std::endl;
            return this;
        }
};

class Foo: public Base {
    public:
        Foo* UniqueFunc() {
            std::cout << "Unique function of the Foo class" << std::endl;
            return this;
        }
};

int main() {
    Foo* exampleFoo = new Foo();
    exampleFoo->UniqueFunc()->UniqueFunc()->CommonFunc()/*->UniqueFunc()*/; // This line doesn't compile with the last function uncommented because CommonFunc() returns a Base* instead of a Foo* and UniqueFunc() is not part of the Base class.

    return 0;
}

/* Output:
Unique function of the Foo class
Unique function of the Foo class
Common function part of Base and all derived classes
[Can't call unique function again or else compile error] */

My solution so far has been to make the base functions virtual and rewrite their definition in every derived class. Not a large rewrite, they execute the same code by simply calling the inherited class in a single line like this Foo* Foo::CommonFunc() {Base::CommonFunc(); return this;}. In the full demonstration, something like this:

#include <iostream>

class Base {
    public:
        virtual Base* CommonFunc() {
            std::cout << "Common function part of Base and all derived classes" << std::endl;
            return this;
        }
};

class Foo: public Base {
    public:
        // Redefine inherited function with the proper return type
        Foo* CommonFunc() {
            std::cout << "Executed from Foo: ";
            Base::CommonFunc();
            return this;
        }
    
        Foo* UniqueFunc() {
            std::cout << "Unique function of the Foo class" << std::endl;
            return this;
        }
};

int main() {
    Foo* exampleFoo = new Foo();
    exampleFoo->UniqueFunc()->UniqueFunc()->CommonFunc()->UniqueFunc(); // This full line now works because Foo::CommonFunc() is redefined to return a Foo* instead of a Base*

    return 0;
}

/* Output:
Unique function of the Foo class
Unique function of the Foo class
Executed from Foo: Common function part of Base and all derived classes
Unique function of the Foo class */

This solution works and does fix the issue, but my project has grown such that the base class has 20 of these self returning functions, and there are 15 derived classes that each need to reroute those functions. If a new function is added to the base class, I need to add new code to every derived class which, while not too terrible, simply defeats the purpose of inheritance. If anyone can help think of a new solution, it would be greatly appreciated!

7
  • 1
    At first glance this looks like a very uncommon design. So I am curious what problem you are tyring to solve. However you could make "UniqueFunc" a virtual function in the base class (that does nothing there) and then override it in the derived class... though in a way that too would be design mess. Commented Jun 29 at 17:04
  • 3
    If you can make Base a template, try CRTP.
    – Evg
    Commented Jun 29 at 17:05
  • 2
    In C++23, Deducing this can replace CRTP Commented Jun 29 at 18:16
  • Overriding functions can already return pointers to types derived from that to which the base-class functions’ return values point. That doesn’t sound like it’s actually useful in this case, though, in that you’d probably want to have Base *p=…; Derived *d=p->CommonFunc();, and that’s impossible. Commented Jun 29 at 19:08
  • "all of these functions return the this keyword" -- More common is to return *this (by reference). It's one extra character in the return statement, but saves one character with each chain (because you'd use . instead of ->).
    – JaMiT
    Commented Jun 29 at 19:19

1 Answer 1

0

If you don't mind a slightly different syntax, you can have this work:

bob->*Foo(a,b,c)->*OtherFunc(x,y,z);

here, Foo and OtherFunc are magic method pointers.

template<class F>
struct magic_method {
  F f;
  template<class X>
  struct invoker {
    X x;
    template<class Lhs>
    friend auto operator->*(Lhs* lhs, invoker const& self) {
      return self.x(lhs);
    }
  };
  auto operator()(auto&&...args)const {
    return invoker{[&](auto* lhs){
      std::ref(f)(lhs, std::forward<decltype(args)>(args)...);
      return lhs;
    }};
  }
};
#define MAGIC_METHOD(NAME) \
  magic_method{ [](auto* self, auto&&...args) { \
    return self->NAME(std::forward<decltype(args)>(args)...); \
  } }

taking your code:

class Base {
public:
    void CommonFunc() {
        std::cout << "Common function part of Base and all derived classes" << std::endl;
    }
};

class Foo: public Base {
public:
    void UniqueFunc() {
        std::cout << "Unique function of the Foo class" << std::endl;
    }
};

we add these magic methods:

const auto CommonFunc = MAGIC_METHOD(CommonFunc);
const auto UniqueFunc = MAGIC_METHOD(UniqueFunc);
 

and it runs as you wish:

int main() {
  Foo* exampleFoo = new Foo();
  exampleFoo->*UniqueFunc()->*UniqueFunc()->*CommonFunc()->*UniqueFunc();
}

the trick is that UniqueFunc(args...) returns an object that binds with ->* and invokes the lhs object with the method named UniqueFunc, passing in args....

It then returns the lhs object regardless of what UniqueFunc returned.

You can also do this with the deduction of this in a later C++ standard, but I don't know the exact syntax.

Not the answer you're looking for? Browse other questions tagged or ask your own question.