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



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.


Get any C++ book (even a very old one) and look for references.

Then meditate about when a copy of an object is made and when not.
[Answer: there exist two reasons for a copy: operator= and a copy constructor]

Perhaps try playing with the attached program. If you understand each line which is printed you made it (a lot of daily C++ programmers still don't fully understand ;-) but the program is a good playground).

   Christof

PS: Short answer: try to avoid copies of Widgets and Lists in general (of course there are a lot of exceptions to this rule). Changing copies does not touch the original.
#include <iostream>

#define DO(x) std::cout << #x << ";\n"; x

class Enlightener
{   int object_num;
    static int counter;
public:
    Enlightener(const Enlightener &b) : object_num(++counter)
    {  std::cout << " copy ctor " << object_num << " <- " << b.object_num << "\n"; }
    Enlightener &operator=(const Enlightener &b) 
    { std::cout << " assignment operator " << object_num << "=" << b.object_num << "\n"; 
      return *this;
    }
    Enlightener() : object_num(++counter)
    { std::cout << " default ctor " << object_num << "\n"; }
    ~Enlightener() { std::cout << " dtor " << object_num << "\n"; }
};

class B  
// try playing with some & or * decoration here 
// it won't crash even when you do the wrong thing !
// (this might be considered a bug)
{  Enlightener e;
public:
   B(Enlightener b) : e(b) {}
};

int Enlightener::counter=0;

void f(const Enlightener &e=Enlightener()) {}
void g(Enlightener=Enlightener()) {}

int main()
{  DO(Enlightener a);
   DO(Enlightener b=a);
   DO(Enlightener c);
   
   DO(c=a);
   DO(Enlightener &d=a);
   DO(Enlightener *e=&a);
   
   DO(f(a));
   DO(f());
   DO(g(b));
   DO(g());
   
   DO({  B f(d)); // to understand these two lines might be tricky
   DO(})
   
   std::cout << "main ends now\n";
}


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