member-function-pointers

cast a pointer to member function in derived class to a pointer to abstract member function

时光毁灭记忆、已成空白 提交于 2021-02-08 06:42:30
问题 I'm trying to do something that seems like it should be fairly common but I've been unable to find anyone discussing it. this post on stackoverflow is similar to what I'm trying to do, but not quite the same. I have an abstract base class: #ifndef _ABASECLASS_H_ #define _ABASECLASS_H_ using namespace std; #include <iostream> #define CALL_MBR_FUNC(object, ptr_to_mem_func) ((object).*(ptr_to_mem_func)) class aBaseClass { public: typedef void (aBaseClass::*aBaseClass_mem_func)(); int A; int B;

cast a pointer to member function in derived class to a pointer to abstract member function

╄→гoц情女王★ 提交于 2021-02-08 06:41:21
问题 I'm trying to do something that seems like it should be fairly common but I've been unable to find anyone discussing it. this post on stackoverflow is similar to what I'm trying to do, but not quite the same. I have an abstract base class: #ifndef _ABASECLASS_H_ #define _ABASECLASS_H_ using namespace std; #include <iostream> #define CALL_MBR_FUNC(object, ptr_to_mem_func) ((object).*(ptr_to_mem_func)) class aBaseClass { public: typedef void (aBaseClass::*aBaseClass_mem_func)(); int A; int B;

casting member function pointer

感情迁移 提交于 2021-02-07 14:48:36
问题 I need to use a member function pointer that takes in an argument of base class that used in other code. Well, simply I want do to [something] like the example below. This code works fine, but I wonder if such cast is always safe? I cannot do dynamic or static cast here. #include <cstdio> class C { public: C () : c('c') {} virtual ~C() {} const char c; }; class D : public C { public: D () : d('d') {} virtual ~D() {} const char d; }; class A { public: A () {} virtual ~A() {} void f( C& c ) {

casting member function pointer

蓝咒 提交于 2021-02-07 14:47:31
问题 I need to use a member function pointer that takes in an argument of base class that used in other code. Well, simply I want do to [something] like the example below. This code works fine, but I wonder if such cast is always safe? I cannot do dynamic or static cast here. #include <cstdio> class C { public: C () : c('c') {} virtual ~C() {} const char c; }; class D : public C { public: D () : d('d') {} virtual ~D() {} const char d; }; class A { public: A () {} virtual ~A() {} void f( C& c ) {

Function taking both pointer to member-function and pointer to const member-function

陌路散爱 提交于 2020-02-27 23:35:25
问题 I have the following code base: template <typename Type> class SomeClass { public: template <typename ReturnType, typename... Params> void register_function(const std::pair<std::string, ReturnType (Type::*)(Params...)> fct) { auto f = [fct](Params... params) -> ReturnType { return (Type().*fct.second)(std::ref(params)...); } // ... } }; This works when I pass a pointer to a member-function (non-const). However, if I want to pass a pointer to a const member-function, it results in a compile

C++ : Getting function virtual 'address' with member function pointer

一曲冷凌霜 提交于 2020-02-05 07:19:44
问题 This question is similar to Print address of virtual member function I would like to retrieve the memory location of a function (in runtime), using a member function pointer. The goal is to log them, and do a post-mortem analysis, using 'ln' in WinDbg to retrieve which function it was, using PDB symbols. I can't use stack walking since I am not yet into the function I want to log. (and I do not want to modify billions of functions to return me their address...). Short sample: class AClass {

Why member function address are so far away from free functions?

社会主义新天地 提交于 2020-02-02 13:07:31
问题 Taking this example: https://godbolt.org/z/gHqCSA #include<iostream> template<typename Return, typename... Args> std::ostream& operator <<(std::ostream& os, Return(*p)(Args...) ) { return os << (void*)p; } template <typename ClassType, typename Return, typename... Args> std::ostream& operator <<(std::ostream& os, Return (ClassType::*p)(Args...) ) { unsigned char* internal_representation = reinterpret_cast<unsigned char*>(&p); os << "0x" << std::hex; for(int i = 0; i < sizeof p; i++) { os <<

Calling derived class through base class function pointer

狂风中的少年 提交于 2020-01-22 18:59:25
问题 Can I call a derived class through a base class function pointer , as shown in the example below? I understand that my example works , but is it guaranteed to always do so (Assuming the object actually implements the function!), or is this just an idiosyncrasy of the compiler I'm using? By this logic can't one simply derive all their classes from "CBase" (which in this case is empty so I guess no overhead) and ignore the type in the function pointer? #include <iostream> struct CBase { };

Why is there an extra & to pass the address of a non-static member function to a thread in C++?

别说谁变了你拦得住时间么 提交于 2020-01-21 07:32:06
问题 As I understand, the name of a function itself serves as a pointer to it. Therefore, when I have a function, I can create a thread by simply passing its address to the thread constructor, like below: void thread_function { } std::thread threadObj1(thread_function); My confusion is while passing the address of a non-static member function to a thread. For example: class ClassA { public: void nonstatic_function() { } }; ClassA instance; std::thread threadObj2(ClassA::nonstatic_function,

passing member-function as argument to function-template

余生长醉 提交于 2020-01-14 13:29:10
问题 Consider three ways to implement a routine in c++: through functors, member functions, and non-member functions. For example, #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class FOO { public: void operator() (string word) // first: functor { cout << word << endl; } void m_function(string word) // second: member-function { cout << word << endl; } } FUNCTOR; void function(string word) // third: non-member function { cout << word << endl; } Now