Re: virtual functions, prototypes, argument passing questions

On Mon, 2011-10-24 at 10:21 -0700, eypros wrote:
> Ok I am little confused. Well I was testing the “Programming with Gtkmm”
> improved Hello World example. I number my question as they came up. There it
> uses a function named 
> virtual void on_button_clicked(Glib::ustring data);

It's not virtual now:

Maybe you are looking at an older version. This was just a habit that I
used to have when writing code. I've changed my mind since then.
However, it's of no great importance.

> 1) Since it's virtual I am wondering where is her prototype? (the base class
> in which it appears first?)

No, it is not a method override. And if it was a method override, it
would not need the "virtual" in the override anyway.

> I searched in Gtk::Button with no success, BUT I found another function
> named:
> virtual void Gtk::Button::on_clicked ( )  [protected, virtual]
> There was no extra information on that function. So I deducted the
> following:
> it's virtual so I can overloaded in my derived class. It takes no argument
> (as in it's prototype there isn't any!). Well wrong (I don't know why
> though).
> 2) Why the prototype lies? Is it because the reference is incomplete so
> there are arguments but they are not written? Or something else?
> I am using the reference for my edition (2.4) in my machine so they should
> be compatible. I tested the example with  on_clicked without argument and 2
> errors appeared. I run it with argument (the same as the other virtual
> function, that is Glib::ustring data) and it worked fine!
> 3) How am I supposed to know that I should use argument in this function
> when it's prototype manifests differently? Where can I get this information
> from?
> 4) The 2 versions (with  on_clicked() and  on_button_clicked()) works fine
> both. So what's the differences between them? How am I supposed to pick one
> or the other?

Gtk::Button::on_clicked() is a method of the button. The
on_button_clicked() in the example is a method of the window. Clearly
you might override Gtk::Button::on_clicked() if you are deriving from
Gtk::Button, but you cannot override it otherwise.

Also, the example shows how the on_button_clicked() signal handler is
connected to the signal in the example:

  sigc::mem_fun(*this, &HelloWorld::on_button_clicked), "button 1"));

> (I have asked this question before but I still don't get the whole picture I
> think)
> We use syntax like:
> m_button2.signal_clicked().connect(sigc::bind<-1, Glib::ustring>(
> sigc::mem_fun(*this, &HelloWorld::on_clicked), "button 1"));

Yes, that uses sigc::bind() to pass an _extra_ argument.

> where I can say that "button 1" is the argument needed in  on_clicked(), and
> also (in the TreeStore example) syntax like this:
> m_TreeView.signal_row_activated().connect(sigc::mem_fun(*this,
>               &ExampleWindow::on_treeview_row_activated) );
> where as I can say THERE ISN'T any argument. BUT there is! First the
> prototype of on_treeview_row_activated says:
> void on_treeview_row_activated(const Gtk::TreeModel::Path& path,
> Gtk::TreeViewColumn* column); 
> Voila 2 arguments!

But there is no need to pass an extra argument. The code that emit()s
the signal passes the standard arguments, not the code that connects to
the signal. You should probably read more about libsigc++ if it confuses
has some links.

> 5) Is this a different way to connect a function to a signal? Is it
> equivalent to a syntax like:
> m_TreeView.signal_row_activated().connect(sigc::mem_fun(*this,
>               &ExampleWindow::on_treeview_row_activated), path,  column);

No, there is no such connect() method. It wouldn't make any sense.

> 6) And how does the argument pass in the first version (that with no
> argument defined) in 
> m_TreeView.signal_row_activated().connect(sigc::mem_fun(*this,
>               &ExampleWindow::on_treeview_row_activated) ); 
> If the arguments are not needed then why they appear in the prototype?

Again, they are sent by the signal emmitter.

> If you can enlighten this issues for me it would be great!

murrayc murrayc com

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]