Showing posts with label LinuxDev. Show all posts
Showing posts with label LinuxDev. Show all posts

Thursday, June 12, 2008

Initialized and Uninitialized Variables

C++ Primer 4/e has such a concept:『Initialization is an important concept in C++ and one to which we will return throughout this book.

Initialized variables are those that are given a value when they are defined. Uninitialized variables are not given an initial value:



   int val1 = 0;     // initialized
int val2; // uninitialized

It is almost always right to give a variable an initial value, but we are not required to do so. When we are certain that the first use of a variable gives it a new value, then there is no need to invent an initial value. For example, our first nontrivial program on page 6 defined uninitialized variables into which we immediately read values.


When we define a variable, we should give it an initial value unless we are certain that the initial value will be overwritten before the variable is used for any other purpose. If we cannot guarantee that the variable will be reset before being read, we should initialize it.』


And Chinese book explains:『初始化(initialization) 在C++ 中是個重要概念,本書將一再提及。


已初始化變數是「定義時便獲得初值」的變數,未初始化變數則沒有獲得初值:



int val1 = 0; //已初始化
int val2; //未初始化
「給變數一個初值」幾乎永遠是正確的行為,但並不是非那麼做不可。如果我們很肯定第一次使用某變數時會給它一個值,那麼就沒有必要為他設想一個初值。例如我們在「p.6」第一個「有所事事」的程式中定義了未初始化變數,隨後立刻把值讀入。

定義變數時應該給予初值,除非我們確定這個初值在該變數第一次被使用前會被覆蓋掉(overwritten)。如果我們無法保證為某變數讀取數值之前該變數會被重新設值(reset),就應該將初始化。』


This concept is useful to me, however in C or other languages, even though php.

Monday, March 17, 2008

G_OBJECT vs GTK_OBJECT

http://ts-hoop.blogspot.com/2008/03/gtkcasting.html has the post ;

看GTK的Casting發現一個小問題


It mentioned the problems of the two types casting, I read the "Foundations of GTK+ Development" and found this is the problems of the versions.

GObject is the fundamental type providing common attributes for all libraries based
on it including GTK+ and Pango. It allows objects derived from it to be constructed,
destroyed, referenced, and unreferenced. It also provides the signal system and object
property functions. You can cast an object as a GObject with G_OBJECT(). If you try to
cast an object with G_OBJECT() that is not a GObject or derived from it, GLib will throw a
critical error, and the cast will fail. This will occur with any other GTK+ casting function.

GtkObject is the base class for all GTK+ objects. It was replaced as the absolute base
class of all objects in GTK+ 2.0, but GtkObject was kept for backward compatibility of
nonwidget classes like GtkAdjustment. You can cast an object as a GtkObject with
GTK_OBJECT().