Re: Java Versus gtk



On Wed, 2004-02-18 at 04:06, Karoliina Salminen wrote:
I don't mean that. I mean that there are built-in classes for many
kind of things in C++ standard library. One example is STL.
For example we needed a linked list to the program. It was easy to 
implement with C and there are millions of examples how to do it,
but it was about 1000 lines of extra code where it would have been
in C++ just one line: std::list<std::string> stringlist;

I personally think that the glib part of gtk gives you almost all the
functionality to do what you've demonstrated using STL.  In some
respects glib is a standard template library.


I haven't seen such
dramatic examples.

One example: A simple dictionary where keyword (string) points
to a list of strings:
Try this with C (how many hundreds of lines of code you'll get since
in C you must implement a tree to accomplish the same efficiency
and in the std::map which used internally a red-black tree):

glib gives stack and vector capability, as well as trees (various
kinds).  I've used glib extensively in a non-gtk application and the
glib routines gave me almost as much power of as STL, all with standard
C.  STL is nice, but sometimes buggy and not always supported by C++
compilers.


#include <string>
#include <vector>
#include <map>
...

std::map<std::string,std::vector<std::string> > stringmap;

std::vector<std::string> translation;
translation.push_back(std::string("tietokone"));
translation.push_back(std::string("tietojenkäsittelylaite"));
stringmap[std::string("computer")]=translation;
std::vector<std::string> diggedFromMap = 
stringmap[std::string("computer")];
std::cout << diggedFromMap[0] << std::endl;
std::cout << diggedFromMap[1] << std::endl;

Maybe I'm not understanding your code, but here goes:

#include <glib.h>

GHashTable *stringmap = g_hash_table_new(g_str_hash,g_str_equal);
GSList *translation = NULL;
GSList *diggedFromMap = NULL;

translation=g_slist_append(translation,"tietokone");
translation=g_slist_append(translation,"tietojenkäsittelylaite");
g_hash_table_insert(stringmap,"computer",translation);
diggedFromMap=g_hash_table_lookup(stringmap,"computer");
g_print("%s\n",diggedFromMap->data);
g_print("%s\n",diggedFromMap->next->data);



Clearly that code is not as robust nor safe (or probably even correct)
but I just wanted to show that C can be almost a flexible as C++.  Also
STL isn't part of C++, but rather a library, just like glib.  If you
pick your library correctly (as in use gtk and glib) I think you can do
just as much as C++, but often in a more portable way.

Michael




(Sorry, I didn't verify if there are typos in my code, it is possible
that it won't work).

Also regard that most accesses to C++ objects are
done technically via pointers as well. Just the notation differs.

Of course there are pointers, but for example dynamic memory allocation
can be left to the problem of the standard library and the user
can write code that never leaks assuming that the standard library
contains no bugs. In STL it is implemented better than in Java.
The garbage collection rarely works right and Java programs tend to leak.
In STL however, memory is freed when a structure goes out of visibility
(past end of the block).



Best Regards,
Karoliina Salminen

/*  
 * | Karoliina Salminen | karoliinasalminen karoliinasalminen com |
 * | http://www.karoliinasalminen.com | http://www.ampcast.com/karoliina |
 *
 */

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
-- 
Michael L Torrie <torriem chem byu edu>



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