PDA

View Full Version : How to pass a member funtion to another object using funtion pointer



Ratheendrans
7th August 2010, 16:29
Hi All,

I want to pass a member funtion of my QWidget class to constructor of my QDialog subclass.

example :

class popup : public QDialog,private Ui::popup
{
Q_OBJECT

public:
popup(QWidget *parent,void (Widget::*)(QString &));
~popup();

};

I get the following error on compilation

error: no matching function for call to ‘popup::popup(Widget* const, <unresolved overloaded function type>)’


Kindly help in fixing these issue.

Ratheendran

bmesing
7th August 2010, 17:06
Your error is caused by calling the constructor, so you need to provide the line where you call the constructor of popup, probably along with the definition of "Widget". Otherwise we cannot help. Btw. your function-pointer has no name...

Btw. this question would have been more appropriate in a C++ forum..

tbscope
7th August 2010, 17:09
And if you tell what you want to do, someone might give an alternative solution.

SixDegrees
7th August 2010, 17:21
In C++, this is not simple, due to encapsulation and inheritance. The syntax becomes rather messy.

For more information, see here (http://www.goingware.com/tips/member-pointers.html).

Many times, a functor can be used in places where a function pointer would otherwise appear. Once in a while, though, only a function pointer will do.

Ratheendrans
7th August 2010, 17:55
Thanks for your suggestions.

this is where I call the constructor of popup and I get the above error message.

void Widget::on_pushButton_clicked()
{
popup popDia(this,putString);
popDia.exec();
}

Just trying to implement a call back in c++.

squidge
7th August 2010, 18:45
What is the definition of 'Widget' ? You seem to be referring to it as a base case, but you don't seem to inherit from it?

SixDegrees
7th August 2010, 20:27
I would recommend using functors, also known as function objects, in place of the cumbersome function pointer syntax. They are perfect for use as callbacks.

Of course, signals and slots do away with much of the need for callback functions; you can simply connect the objects that need to communicate.

bmesing
8th August 2010, 09:05
You still haven't provided the definition of Widget, so its hard to help. From the error message I would guess, that you have two putString functions in Widget, and hence the compiler is unable to resolve which one to take.
As an alternative approach, apart from a functor (function object) you could also use the listener concept (i.e. observer pattern).