C ++ Get class type inside static function

Inside the static member function I need to get the type.

class MyClass { public: static void myStaticFunc(); ... }; 

And then in the implementation I want:

 void MyClass::myStaticFunc() { // Get MyClass as a type so I can cast using it (get_type_from_static_function()*)someOtherVariable; } 

Is it possible? Normally I would use something from typeinfo for the object, but I don't have this .

I don't want to just use (MyClass*) , because it happens inside a macro, and I would like to keep it as simple as possible so that it can be called without a class name.

If this helps, I use QT, but I could not find any macros to get the current class. It does not have to be software - it can be a macro.

Hooray!

EDIT: Here is the actual macro function:

 #define RPC_FUNCTION(funcName) \ static void rpc_##funcName(void* oOwner, RpcManager::RpcParamsContainer params){ ((__class__*)oOwner)->funcName(params); }; \ void funcName(RpcManager::RpcParamsContainer params); 

Then I call RPC_FUNCTION(foo) in the class declaration. I want __class__ be any declaration of the class I was in. I am well aware that I can just add className after funcName, but I want it to be as simple as possible when using. My RPC manager calls rpc_foo and passes a pointer to the class object in which I declared it. Essentially, I need to know how to determine the actual class of this parameter void *.

+4
source share
6 answers

I believe that what you ask at heart is simply not possible: C ++ is a statically typed language, which means that all type information should be available at compile time (despite polymorphism at runtime). That is, when you say

 T x; 

then type T must be known at compile time. There is no such thing as " T_from_user() x; " in which the actual type of the variable is determined at run time. The language is simply not designed that way.

Usually, if you ask such a question that the indicator you are making about the problem is wrong. Typical solutions for polymorphic situations include class inheritance and virtual functions or other kinds of lookup tables, or indeed any number of different approaches. Your request for a preprocessor macro also indicates that something is disabled. Any programming language has its own idioms, and deviating from them too much is a bad idea.

+1
source

What you want to do is called Reflection. It was implemented in .NET (I don’t know, maybe in Java too) and will be implemented in future C ++ standards.

0
source

It seems that you have several unrelated classes that have a number of common methods (those that can be sent as an argument to funcName in your example).

Instead of having these unrelated classes, consider a polymorphic approach. For example, suppose the functions you support are func1 and func2 , then you can do this as follows:

 class BaseClass { public: virtual void func1(RpcManager::RpcParamsContainer args) = 0; virtual void func2(RpcManager::RpcParamsContainer args) = 0; }; class MyClass1 : public BaseClass { public: virtual void func1(RpcManager::RpcParamsContainer args) { /* func1 implementation here */ } virtual void func2(RpcManager::RpcParamsContainer args) { /* func2 implementation here */ } }; class MyClass2 : public BaseClass { public: virtual void func1(RpcManager::RpcParamsContainer args) { /* func1 implementation here */ } virtual void func2(RpcManager::RpcParamsContainer args) { /* func2 implementation here */ } }; 

In the above design, you can pass BaseClass* around, and you can call func1 or func2 without having to do any casts, and the compiler will find the right version to call. For example, in your macro, you can do something like this:

 #define RPC_FUNCTION(funcName) static void rpc_##funcName(BaseClass* oOwner, RpcManager::RpcParamsContainer params){ oOwner->funcName(params); }; 

Hope this helps!

0
source

Looking for function macros? This is a macro that expands to the current function name.

  __FUNCTION__ 
0
source

In Visual Studio 2012, you can use this trick, but it will not work in gcc, at least for now.

  template<typename base_t> static auto GetFunctionBaseType(void(base_t::*)())->base_t; struct TBase { template<typename T> void GetBaseType(); typedef decltype(GetFunctionBaseType(&GetBaseType<void>)) this_t; static void rpc_func1(void * ptr) { ((this_t*)ptr)->func1(); } }; 
0
source

No, a static method can only see static members of a class. For him, it makes no sense to refer to the members of the instance (as, for example, in standard variables, etc.), since they do not exist if the class was not instantiated.

It seems that you want something like a Singleton design template. This allows you to use only one instance of the class at a time.

Another way would be to have a static list of all instances of the class, and then in the class constructor add the this pointer to this list. As I said, static members cannot access instance variables since they may not exist at all.

I believe the more important question is: why do you need to access the instance variable from a static member? If you need access to an instance member, you must call the function in the context of the current instance, otherwise you are pretty much violating the OOP paradigm.

-1
source

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


All Articles