3

In Delphi there are exists class helpers which can add methods for some given class.

Is there any design pattern in C++ which can do the same?

vladon
  • 8,158
  • 2
  • 47
  • 91
  • 2
    From the usage in the link, the closest thing I can think of is free functions that have that class as a parameter. If the unified call syntax proposal goes through, you could eventually be able to call those with the same syntax as if they were member functions. – chris Jul 15 '15 at 07:26
  • 1
    Why somebody minuses this question? Is it incorrect? – vladon Jul 15 '15 at 07:29
  • (Not my downvote) In C++, you can derive from `struct` whereas you can't derive from Delphi's `Record`. That takes away one of the justifications. – MSalters Jul 15 '15 at 07:42
  • @MSalters let's forget about records/structs, let's talk about classes. – vladon Jul 15 '15 at 07:43
  • @chris can you please write an example of calling as if they were member functions? – vladon Jul 15 '15 at 07:44
  • @vladon, It's really what you'd expect. `struct Foo {}; void bar(Foo foo) {...} int main() {Foo foo; foo.bar();}`. Note that I said *if the proposal goes through*. There is (was?) more than one. [Here's Bjarne's](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4174.pdf). Of course it's possible right now to call `bar(foo);`, which really should not be unacceptable since it accomplishes the same extension of the class. – chris Jul 15 '15 at 07:44
  • @chris I cannot get it. Suppose I have a library class `TStreamBuf`. And I want to add method to it. In Delphi I can use class helper: `TStreambufHelper = class helper for TStreamBuf` and define methods there: `TStreamBufHelper.MyMethod` and call it like `TStreamBuffer.MyMethod()`. How to do it in C++? – vladon Jul 15 '15 at 07:47
  • [Extension Methods – A Polished C++ Feature](https://vivekragunathan.wordpress.com/2008/04/09/extension-methods-a-polished-c-feature/). – LU RD Jul 15 '15 at 08:09
  • @LURD: That article merely explains that extension methods are simply an application of the C++ idiom "Koenig lookup". I don't quite agree, but that is what the article claims. It does not claim you can do exactly the same as extension methods or class helpers in standard C++. – Rudy Velthuis Jul 15 '15 at 13:48
  • @MSalters: It only takes away some of it. You can derive from classes in Delphi, just like in C++. And we are discussing class helpers, after all. Class helpers merely allow a syntax that makes it look as if the extended method is part of the public interface. It is not absolutely necesary, and the same can be done with a simple non-method procedure or function with an object of the given type as first parameter. But then it is not officially an extension method (or class helper) anymore, although behind the scenes, there is no difference. – Rudy Velthuis Jul 15 '15 at 13:53
  • @vladon Are you still in any doubt? – David Heffernan Jul 17 '15 at 04:37
  • @DavidHeffernan It's no doubt, it is more theoretical question ("Is it possible to make analogue of Delphi class helper in C++?") – vladon Jul 18 '15 at 07:31
  • You've had your answer two days ago. I wonder what more you are looking for. – David Heffernan Jul 18 '15 at 07:33
  • @DavidHeffernan It is not an _answer_ to the question. – vladon Jul 18 '15 at 07:33
  • It certainly is. What you are trying to do cannot be done in C++. – David Heffernan Jul 18 '15 at 07:42

1 Answers1

2

In .net you have extension methods which are similar. In Python you can use monkey patching which is similar. In standard C++ there is nothing similar to Delphi class helpers.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I'm only a casual reader of C++, but it seems to be possible to use extension methods in C++. See my link in the comment to the question. – LU RD Jul 15 '15 at 08:13
  • @LURD That's a non-member function that accepts an object as its first parameter. Instead of `Foo(sc)`, a true analogue would require `sc.Foo()`. – David Heffernan Jul 15 '15 at 08:17
  • Yes it seems so, It was discussed here, [Extension methods in C++](http://mariusbancila.ro/blog/2014/10/15/extension-methods-in-cpp/). – LU RD Jul 15 '15 at 08:19