Re: Best code practice for new objects



On Sun, 2008-05-18 at 12:54 +0200, Nicola Fontana wrote:
Hi,

trying to adopt the 'best' way to implement my own GObject based
objects, it turns out that there is more than one way to do it.

even if there is more than one way to do something, it does not mean
that every way is the right way. :-)

My idea is to use g_type_class_add_private() and write set/get
public methods in the class structure for every public property,
but this will leave the instance structure as:

struct _MyInstance
{
  MyParent *parent;
};

no, it will be:

  struct _MyInstance
  {
    MyParent parent_instance;
  };

otherwise it will not work (I'm pedantic, I know, but the mailing list
is public, and so are its archives, and even having typos in the code
makes it hard to find the right information at times).

for every object, and this sounds me quite weird.

why?

Is this a common approach?

yes, if you plan to only have accessor functions. you might provide
structure members, but that will make it difficult to change them, in
case you plan to restructure your API.

Is there an official/preferred/best way to do this?

you can also have a:

  MyInstancePrivate *priv;

pointer, to avoid the calls to G_TYPE_INSTANCE_GET_PRIVATE() every time
you need to access the private data.

remember: the instance structure should be as opaque as possible.

ciao,
 Emmanuele.

-- 
Emmanuele Bassi,
W: http://www.emmanuelebassi.net
B: http://log.emmanuelebassi.net




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