Is it possible in C++ to create a function which is not defined in class A but can be treated like a method pointer? Eg.:
typedef bool (A::*MethodType)(int a);
MethodType g_someMethod = &A::SomeMethod;
Now, I want to create a new function AnotherMethod which is of the type MethodType. I have tried to do the following:
bool A_AnotherMethod(A* _this, int a) {
std::cout << __FUNCTION__ << "\n";
return true;
}
MethodType g_someMethod = A_AnotherMethod;
// ...
(this->*g_someMethod )(42);
But I get
error C2440: '=' : cannot convert from 'bool (__cdecl *)(A *,int)' to 'bool (__cdecl A::* )(int)'
How to do it correctly?
No, you can't. C++ does not have a feature similar to extension methods in C#.
p.s. Method pointers have clumsy syntax in C++ and are rarely used. But this is the way how they are defined in the language.
Related
I have a function that takes following arguments as below -
int X::MainFun(Pfn, void*,int)
and Pfn is defined as
int (*Pfn) (int*, void*, int);
Now I have a function template as follows :
template <typename IntfType>
static int MasterOps(int * pRec, void* pSt, int pOffset ){
IntfType* IfObj = (IntfType*) (((MasterOpArgs*)pSt)->vIfObj);
if(IfObj->MasterOps(pRec, pSt, pOffset) == MIGINTF_SUCC) return 1;
else return 0;
}
Now I am calling function MainFun as -
MasterOpArgs margs;
Xobj->MainFun(MasterOps<MigT>, &margs,0 );
type MigT is a class defined earlier which implements a member MasterOps and MainFun() is actually a member function of some class.
Definition of template and call for MainFun() is happening in same file.
I assumed that call to MainFun would also cause instantiation of function template MasterOps with type MigT, but it seems I am wrong as I see following errors
error C2440: 'type cast': cannot convert from 'overloaded-function' to 'Pfn': note: None of the functions with this name in scope match the target type
error C2059: syntax error: ','
Here is the code:
vector<ClientInfo*> OpenRABiz::GetHumans() const {
vector<ClientInfo*> vec;
for (auto &c : clients) {
if (!c.isbot) {
vec.push_back(&c);
}
}
return vec; // RVO - return value optimization
}
In visual c++ 2019, compiler indate it:
error C2664: 'void std::vector<ClientInfo *,std::allocator<ClientInfo *>>::push_back(_Ty &&)': cannot convert argument 1 from 'const ClientInfo *' to 'const _Ty &'
The error message: "
const _Ty&" means the c++ template can't deduce the right parameters
.
When I take the const keyword, it compiles successfully.
vector<ClientInfo*> OpenRABiz::GetHumans()
Why?
Your clients is likely a vector of ClientInfo, so in a const-qualified member-functions, the type of client (in the loop) is const ClientInfo&. When you take the address &client, you get a const ClientInfo*, which cannot be converted to a ClientInfo*.
When you remove the const-qualifier, everything works fine because client is then ClientInfo&.
To fix the issue, change the return-type of the function and declaration of vec to std::vector<const ClientInfo*>.
I've been trying to make a pointer function that points to a method doing something like this(visual C++):
struct test
{
int tst(int a)
{
return a * 4;
}
};
// ok, this the visual C++ compiler does not accept it... (mingw accept it)
int(*tstptr)(int) = (int(*)(int))&test::tst;
Then I've done something like this:
struct Dx
{
int SomeMethod()
{
return 4;
}
};
struct Dy
{
static int(*pSomeMethod)();
};
int(Dy::*pSomeMethod)() = (int( Dy::*)())&Dx::SomeMethod;
So far so good, this compiles without problems, but if I try call her:
Dy::pSomeMethod();
The compiler returns me:
Error 1 error LNK2001: external symbol "public: static int (__stdcall
* Dy::pSomeMethod) (void)" (? PSomeMethod#Dy##2P6GHXZA) unresolved
which I do not understand, because it is not suppose pSomeMethod he is not pointing at SomeMethod ?
The type of &test::tst is int (test::*) (int), which is a member function pointer.
You are trying to convert it to a regular pointer type, which is not possible because they are completely different.
That's why you will have this type cast error:
error C2440: 'type cast' :
cannot convert from 'int (__thiscall test::* )(int)' to 'int (__cdecl *)(int)'
int(Dy::*pSomeMethod)() = (int( Dy::*)())&Dx::SomeMethod;
Type Checked, so no complain from compiler.
As for:
Dy::pSomeMethod();
This a __cdecl.
But, SomeMethod is a __thiscall, which means it's really like this
int SomeMethod( Dx &this);
So, linker can't find a match.
You simply can't call non-static method without object of class Dx.
Your declaration of pSomeMethod defines a pointer to a function within class Dy that returns an int. You want to declare it as it appears in the linker error:
int (*Dy::pSomeMethod)();
which is a member of Dy that is a pointer to a function returning an int.
What you're trying to do won't work, since Dx::SomeMethod is a member function of Dx, which needs a this pointer. Calling thru pSomeMethod won't have one.
When done right, you can just assign the address of the function to the pointer without using a cast.
I'm trying to expose a overloaded function using boost::python.
the function prototypes are:
#define FMS_lvl2_DLL_API __declspec(dllexport)
void FMS_lvl2_DLL_API write(const char *key, const char* data);
void FMS_lvl2_DLL_API write(string& key, const char* data);
void FMS_lvl2_DLL_API write(int key, const char *data);
I'v seen this answer: How do I specify a pointer to an overloaded function?
doing this:
BOOST_PYTHON_MODULE(python_bridge)
{
class_<FMS_logic::logical_file, boost::noncopyable>("logical_file")
.def("write", static_cast<void (*)(const char *, const char *)>( &FMS_logic::logical_file::write))
;
}
results with the following error:
error C2440: 'static_cast' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(const char *,const char *)'
None of the functions with this name in scope match the target type
trying the following:
void (*f)(const char *, const char *) = &FMS_logic::logical_file::write;
results:
error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(const char *,const char *)'
None of the functions with this name in scope match the target type
what's wrong and how to fix it?
EDIT
I forgotten to mention a few things:
I'm using vs2010 pro on win-7
write is a member function of logical_file
FMS_logic is a namespace
Well the second attemp should work, if write is a pure function. From your code it seems you do have a memberfunction. Pointers to member-functions are ugly, you'd rather use a function object.
However: you would have to post the whole code, it is not clear whether write is a member-function or not.
Edit: if it is a member-function of FMS_logic::logical_file the syntax would be:
void (FMS_logic::logical_file::*f)(const char *, const char *) = &FMS_logic::logical_file::write;
This just applies for non-static member function, i.e. if a function is static or logical_file is just a namespace it is as you wrote it before.
Your code doesn't work because your function pointer type is wrong. You need to include all type qualifiers (your DLL qualifier is missing) and, as Klemens said, the class name. Putting this together, your code should read
.def("write", static_cast<void FMS_lvl2_DLL_API
(FMS_logic::logical_file::*)(const char *, const char *)>
(&FMS_logic::logical_file::write))
Thanks for the hint with the static_cast<>, I had the same problem as you, just without the dllexport, and after adding the static_cast it works :-)
I know that the title is not very clear but I didn't know how to write it down in one sentence. So the problem is that I want something like this:
void(typeof(this)::*function)(int,int);
I know that is not going to work but I was wandering whether a solution exists for this problem in c++ or not?
Update:
class MainPage
{
public:
MainPage()
{
void (std::remove_reference<decltype(*this)>::*callback)(int, int) = &MainPage::myFunction;
((*this).*callback)(nullptr,nullptr);
}
~MainPage()
{
}
void myFunction(int a, int b)
{
}
}
Errors:
error C2440: 'newline' : cannot convert from 'MainPage *' to 'std::remove_reference<_Ty> *'
error C2647: '.*' : cannot dereference a 'void (__thiscall std::remove_reference<_Ty>::* )(int,int)' on a 'MainPage'
Yes, use decltype:
void (std::remove_reference<decltype(*this)>::type::*function)(int, int);