0

Is it possible in C to do method chaining in the following way:

// is this the closest to item.find('path').find('subpath') ?
item -> find('path') -> find('subpath')

Or is this construction not really possible? If it's possible (or a variation of it), what might be the most basic example?

// like an XML hierarchical path
typedef struct Item {
    char *path;
    find (??)
} Path;
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
  • C doesn't have methods. Are you sure you don't mean C++? – Barmar Dec 10 '20 at 05:25
  • @Barmar yea, I've never written C++ -- I mean the C functions. – samuelbrody1249 Dec 10 '20 at 05:26
  • You can put function pointers in structures. `struct Item* (*find)(char *);` – Barmar Dec 10 '20 at 05:27
  • 1
    But unlike methods, the function doesn't automatically receive the object that pointed to it as an argument. So there's no way for it to know which `Item` you're searching. – Barmar Dec 10 '20 at 05:29
  • Literally the struct of function pointers won't really allows multiple member function pointers to be chained as `a->func1()->func2()` without a further cludge of the function returning a pointer to that type struct which would then be further chained.. You can use one member access with `->`, but multiple wold fail because, e.g. `a->func1()` doesn't have a member named `func2()`. C has no built-in syntax for function chaining with `->`. – David C. Rankin Dec 10 '20 at 05:32

0 Answers0