Re: [Re: [gtkmm] gtkmm2: no icons in stock menu items?]



On Thu, 2002-06-27 at 23:52, Spandan Bhatt wrote:
> 
> --- Christof Petig <christof petig-baender de> wrote:
> > 
> >   MenuList &list_view=view_menu->items();
> Wow!! can you provide explanation for this perticular
> usage(a link would be great)
> I am talking about the C++ part of it.

It's clearer if you write it this way:

MenuList& list_view=view_menu->items();

list_view is a reference (which is in essence a pointer that has the
syntax of a struct) to the MenuList of the view_menu object.  So when
you modify list_view, you're really modifying the view_menu member data
(remember the pointer bit).

Technically, you should use it:

MenuList& list_view(view_menu->items());

The compiler sort of re-interprets the assignment in the ctor as a
copy-ctor when constructing references, because a reference must
*always* exist, and to rewrite the first example as:

MenuList& list_view;  list_view = view_menu->items();

just doesn't make sense.  After the construction, list_view points
nowhere.

It's also clearer in your mind, because if you do something later like:

list_view = other_view_menu->items();

for example to start modifying other_view_menu, in reality you have just
done:

view_menu->items() = other_view_menu->items();

since you can't reassign a reference to refer to a different object once
constructed.

Of course, this is all laid out in any good C++ book, Stroustrop's would
be the first that comes to mind.

Regards,
Carl





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