Dynamic proxy classes in C ++. Is it possible?

First of all, let me say that I am not a C ++ programmer. I have several years of experience in C ++, but my main area is.NET/C#.

I am looking for a way to create dynamic proxy / wrapper classes in C ++. In particular, I want to achieve interception of a method call. Such tricks are common in the Java / .NET world, but in C ++ there is no Reflection.

I found an online tutorial that explains how to create shell calls and intercept through β†’ operator overloading:

class Person{ std::string mName; Person(std::string pName): mName(name){} void printName(){ std::cout << mName << std::endl; } }; template <class T > class Wrap { T * p ; public: Wrap (T * pp ) :p (pp) { } Call_proxy <T> operator ->() { prefix (); return Call_proxy<T>(p); } }; template <class T > class Call_proxy { T * p ; public : Call_proxy (T * pp ) :p (pp ){ } ˜Call_proxy () { suffix (); } T * operator ->() { return p ; } }; 

As you can see from this example, we can catch method calls before and after the call, but it is not clear to me how to determine which method is being called? Is it possible at all?

thanks

UPDATE

Well, to make things clearer, I don't care if the implementation is really dynamic or not. Having a wrapper class like smart pointers is right for me.

+4
source share
1 answer

No. It is specially designed to be non-intrusive. All wrappers are the easiest way to call the prefix and suffix, and then return the referenced object so that it can call the specified function. If the operator β†’ is overloaded, then the function object-> function () expands to object.operator β†’ () β†’ function ().

Here is a link to an article written by Straustup, it is very informative http://www.stroustrup.com/wrapper.pdf

+1
source

Source: https://habr.com/ru/post/1446947/


All Articles