# need help with understanding wicked syntax in gtkmm



## lockfile (Dec 17, 2012)

I have read two C++ books from back to front. I swear. But the syntax in gtkmm tutorial threw my brain out because I have never seen this c++ syntax before.

It is: 


```
Gtk::DrawingArea myArea;
Cairo::RefPtr<Cairo::Context> myContext = myArea.get_window()->create_cairo_context();
myContext->set_source_rgb(1.0, 0.0, 0.0);
myContext->set_line_width(2.0);
```

I understand everything except for the second line. From my c++ understanding myArea's public method is being accessed but then the code implies that the function is an object (or structure itself) that then points to another function. Or is the function being treated like a variable and then accessing a function within that variable? 

What the heck is going on here?


----------



## kpa (Dec 17, 2012)

Nothing more complicated than two method calls, first trough an object and the second via a pointer to an object.

First the get_window() method of myArea is called. The result of that call is an object that has the -> operator defined, in other words it behaves like a pointer (it can be a C pointer or something more complex, it doesn't matter). Then the pointer is dereferenced (the ->) to get the object pointed to and the method create_cairo_context() of that object is called the get the final result of the expression.

If it helps you to understand it better, here is the same expression with the arrow (->) operator replaced with equivalent dereference of pointer (*) and method selection (.) operator:


```
Cairo::RefPtr<Cairo::Context> myContext = *(myArea.get_window()).create_cairo_context();
```


----------



## lockfile (Dec 17, 2012)

Okay. I get it now. It's sort of like overloading ostream and istream objects with << and >>. Uh wow I have seen the semantics of that before but not that syntax :/

Thank you.


----------



## expl (Dec 17, 2012)

lockfile said:
			
		

> Okay. I get it now. It's sort of like overloading ostream and istream objects with << and >>. Uh wow I have seen the semantics of that before but not that syntax :/
> 
> Thank you.



It has nothing to do with overloading, not sure how did you get that impression. It is more about using custom operators to do the magic.


```
Cairo::RefPtr<Cairo::Context> myContext = myArea.get_window()->create_cairo_context();
```

You copy the context object from the pointer to your local class object, because Cairo::RefPtr uses custom '=' operator that does the memory copy from target.

Cairo::RefPtr also has custom '->' operator that allows to access members of objects it encapsulates.

I recommend you to use properly configured IDE to learn APIs like gtkmm as you can browse through API headers fast and look up the declarations.


----------



## lockfile (Dec 22, 2012)

I meant to say operator overloading not function overloading (there is a difference, -> and >> and + are operators while some myfav( arg1, arg2) is a method).

C++ has some language inconsistency when it comes to the OO features of the language. People complained about C's incantations when working with C in OO paradigm (notorious Gobject, GNOME).

Oh and the formatting looks weird with the small columns because I'm on my phone using Opera Mini. So don't blame me. 

Anyway, I'll mark this thread as solved.


----------

