Unknown Syntax void (Type::m_function)() - c++

I am currently reading through some code and came across a few lines I do not understand.
First
void Foo()
{
(((Type*)parent)->*m_function)();
}
As far as I can tell they are casting the parent to Type and then calling a dereferenced function? I am not sure I have seen the ->*m_function before.
Also I can not see where m_function is declared perhaps here? Which contains more syntax I do not understand. Is it declaring a function that returns void and takes a parameter of a function? But where is the function name?
class Foo()
{
void (Type::*m_function)();
};

It's calling a member function using a pointer to this function: C++ Call Pointer To Member Function
Yes, void (Type::*m_function)(); declares m_function member of type "member function of Type taking 0 args and returning void"

Related

C++ object reference

I have this piece of code:
Wmmbid02 wmmbid02;
wmmbid02.Omplir(this);
return wmmbid02;
and Wmmbid02.h:
class Wmmbid02: public Idoc {
public:
Wmmbid02();
//void EscriureFitxer();
void Omplir(Edi);
private:
Segment crearSegment(string a[], string b[]);
Info crearInfo(string);
//virtual void LlegirFormat(string);
};
Visual Studio, returns and error because can't convert 'Edi *const ' to 'Edi'.
What is the correct answer???
Thanks
Your declaration of Omplir
void Omplir(Edi);
says that this method take an Edi object.
In C++ this is a pointer. You should use it inside a class.
If you want it to work, you should dereference it:
wmmbid02.Omplir(*this);
The standard says about the this pointer:
9.3.2 The this pointer [class.this]
In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, [...]
So in your case, you seem to be in a member function of the class Edi, this should be of the type Edi*.
Without seeing the definition for this I'd say you'll probably want to replace void Omplir(Edi); by void Omplir(Edi* const); and adapt the definition of Omplir accordingly.
Either define the parameter void Omplir(Edi) to a pointer:
void Omplir(Edi*);
Or pass this as reference:
Wmmbid02 wmmbid02;
wmmbid02.Omplir( *this );
return wmmbid02;
But it really depends on what you exactly want to do. Usually i prefer passing by references whenever possible.

Passing a function as an argument in c++

I have the following call:
void Derived::GetEntry(Skill&);
InorderTraverse(GetEntry);
Which calls
void Base<ItemType>::InorderTraverse(void Visit(ItemType&)) const
Attempting to compile as written generates
error C3867: 'Derived::GetEntry': function call missing argument list; use '&Derived::GetEntry' to create a pointer to member
Using &Derived::GetEntry generates
cannot convert parameter 1 from 'void (__thiscall Derived::* )(Skill &)' to 'void (__cdecl *)(ItemType &)'
Changing the declaration to static void GetEntry... fixes these problems, but creates a new set of problems (namely that I can't access non-static objects (nonstatic member reference must be relative to a specific object)
I have a similar traversal operation that works fine with a static declaration, since the called function just displays information about each object on which it is called.
I've been searching for a few days now for an answer, and I feel like it's something simple. Is there a way to use a nonstatic function as a parameter in another function call?
The complete code is: https://github.com/mindaika/SkillTree
Edit: Inserted full working example instead.
I suggest you use the std function pointers instead.
For instance:
#include <functional>
void main()
{
class TheClass
{
public:
TheClass()
{
m_function = ( std::tr1::bind(&TheClass::run, this) );
};
void run()
{
// stuff to do
};
std::tr1::function<void ()> m_function;
};
TheClass theclass;
theclass.m_function();
}
m_function (); // call function
As I mentioned in my question, I was trying to get around the "memory leak" caused by using a static variable (it's not actually a leak, since statics are destroyed, but not until after the leak detector runs). I ended up using a modification of the encapsulated pointer described here: C++ freeing static variables

c++ member function pointer problem

I'm new to c++ . I want to know about object pointer and pointer to member function . I wrote a code which is following:
code :
#include <iostream>
using namespace std;
class golu
{
int i;
public:
void man()
{
cout<<"\ntry to learn \n";
}
};
int main()
{
golu m, *n;
void golu:: *t =&golu::man(); //making pointer to member function
n=&m;//confused is it object pointer
n->*t();
}
but when i compile it it shows me two error which is following:
pcc.cpp: In function ‘int main()’:
pcc.cpp:15: error: cannot declare pointer to ‘void’ member
pcc.cpp:15: error: cannot call member function ‘void golu::man()’ without object
pcc.cpp:18: error: ‘t’ cannot be used as a function.
my question are following :
What I'm doing wrong in this code ?
How to make object pointer ?
How to make pointer to member function of a class and how to use them ?
Please explain me these concept.
Two errors corrected here:
int main()
{
golu m, *n;
void (golu::*t)() =&golu::man;
n=&m;
(n->*t)();
}
you want a pointer to function
the priority of the operators is not the one you expected, I had to add parenthesis. n->*t(); is interpreted as (n->*(t())) while you want (n->*t)();
A member function pointer has the following form:
R (C::*Name)(Args...)
Where R is the return type, C is the class type and Args... are any possible parameters to the function (or none).
With that knowledge, your pointer should look like this:
void (golu::*t)() = &golu::man;
Note the missing () after the member function. That would try to call the member function pointer you just got and thats not possible without an object.
Now, that gets much more readable with a simple typedef:
typedef void (golu::*golu_memfun)();
golu_memfun t = &golu::man;
Finally, you don't need a pointer to an object to use member functions, but you need parenthesis:
golu m;
typedef void (golu::*golu_memfun)();
golu_memfun t = &golu::man;
(m.*t)();
The parenthesis are important because the () operator (function call) has a higher priority (also called precedence) than the .* (and ->*) operator.
'void golu:: *t =&golu::man();' should be changed to 'void (golu:: *t)() =&golu::man;' you are trying to use pointer to function not pointer to result of a static function!
(1) Function pointer is not declared properly.
(2) You should declare like this:
void (golu::*t) () = &golu::man;
(3) Member function pointer should be used with object of the class.

Help needed in Use of Function pointer in C++

i have made a sample example, in this i'm trying to pass a function as argument i am getting error, could you please help me
typedef void (*callbackptr)(int,int);
class Myfirst
{
public:
Myfirst();
~Myfirst();
void add(int i,callbackptr ptr)
{
ptr(i,3);
}
};
class Mysec
{
public:
Myfirst first_ptr;
Mysec();
~Mysec();
void TestCallback()
{
callbackptr pass_ptr = NULL;
pass_ptr = &Mysec::Testing;
first_ptr.add(2,&Mysec::Testing);
}
void Testing(int a,int b)
{
int c = a+b;
}
};
The type of the callback function you're passing as parameter is not defined as part of a class. You probably should define Testing as static.
You are geting an error because you are pointing to a member function. Pointers to member functions are different. See here:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.1
A member function needs to know what instance it is working with (the this pointer) so it can't be called like any other function. If you moved the callback function out of the class (or made it static, which is similar to moving it out of the class) you could call it like any other function.
A more modern way of doing this is to use functors, e.g. boost::function and something like boost::bind :
C++ Functors - and their uses
how boost::function and boost::bind work
Those can hide the difference between member and global functions.
You are trying to access a member function pointer here, using a simple function pointer typedef, which will not work. Let me explain.
When you write a normal, non-member function (similar to C), the function's code actually exists in a location indicated by the name of the function - which you would pass to a function pointer parameter.
However, in the case of a member function, all you have is the class definition; you don't have the actual instance of the class allocated in memory yet. In such a function, since the this pointer is not yet defined, any reference to member variables wouldn't make sense, since the compiler doesn't have enough information to resolve their memory locations. In fact, member function pointers are not exact addresses; they encode more information than that (which may not be visible to you). For more, read Pointers to Member Functions.

Providing an instanceable class's method as a function pointer?

Here is what I'd like to do. I have a function pointer which wants a function like this:
void func(int a);
so I have a class:
class Foo {
public:
void func(int a);
};
Foo *foo = new Foo;
something->setFunction(foo->func);
or in my case:
testWidget[count] = new TestWidget;
testWidget[count]->eventMouseClick.addHandler(testWidget[0]->silly);
But this gives me:
Error 5 error C3867:
'TestWidget::silly': function call
missing argument list; use
'&TestWidget::silly' to create a
pointer to
member c:\users\josh\documents\visual
studio
2008\projects\agui\alleg_5\main.cpp 190
Is there a way I could make this work without using a static function?
Thanks
Is there a way I could make this work without using a static function?
No. You can't convert a pointer to member function to an ordinary function pointer.
If the callback accepted any callable object (or a std::function, for example), then you could bind the object to the member function and pass the result of that; unfortunately, you can't convert that result to an ordinary function pointer, though.
Your question is not very clear to me. But the error message from the compiler makes me feel that probably you want something like this something->setFunction(&foo->func);
An example might help in case of overloads
struct T{
void func(int){}
void func(double){}
};
void f(void (T::*f)(int)){}
void f(void (T::*f)(double)){}
int main(){
void (T::*fi)(int) = &T::func;
void (T::*fd)(double) = &T::func;
f(fi);
f(fd);
}