在這一部分之前,書中介紹了基本類型的顯式初始化以及簡單的異常處理.
基本類型的顯式初始化是比較簡單的.就是說你在定義一個整型變量的時候,有兩種不同的情況:
int i1; // undefined value
int i2 = int(); // initialized with zero
如果按照前一種,會作"值未定義;如果按照后一種,則自動被初始化為0.這樣也就確保了你的類在初始化的時候有一個確定的初始值.
至于異常的處理等問題,書中會在后面有比較詳細的描述.這里可以看到比較有意思的一點,就是指定函數拋出的異常類型,這于Java很像:
void f() throw(bad_alloc);
下面轉入正題:命名空間.
有了命名空間,它將會取代函數和類作用于全局,并作為它所統領的那些類和函數的唯一標識存在.這樣可以避免命名沖突情況的出現.正如書中所說:
Unlike classes, namespaces are open for definitions and extensions in different modules. Thus
you can use namespaces to define modules, libraries, or components even by using multiple
files. A namespace defines logical modules instead of physical modules (in UML and other
modeling notations, a module is also called a package).
可以像這樣定義一個命名空間:
namespace MyNameSpace
{
class MyClass
{
private:
char * _classInfo;
public:
char* getClassInfo()
{
return _classInfo;
}
MyClass(const char* info)
{
_classInfo=new char[strlen(info)];
strcpy(_classInfo,info);
}
~MyClass()
{
if(_classInfo)
{
std::cout<<"free classinfo";
delete[] _classInfo;
}
}
};
void printMyClassInfo(MyClass &instance)
{
std::cout<<instance.getClassInfo();
}
}
從上面可以看出,這個命名空間里面包括了一個類和一個函數.類中包含了char*類型的成員變量.函數printMyClassInfo 以一個MyClass類型的引用作為參數.為什么要用引用呢?熟悉c++的人應當很清楚,我是通過實驗才剛剛知道原因.這個原因我將會在后面說明.
好現在來看一下調用過程,通常的調用過程是這樣的:
int main()
{
MyNameSpace::MyClass instance("MyClass!\n");
MyNameSpace::printMyClassInfo(instance);
}
這沒有任何問題,但有意思的是,還可以這樣調用:
int main()
{
MyNameSpace::MyClass instance("MyClass!\n");
printMyClassInfo(instance);
}
看來c++中在使用一個命名空間的類或者函數的時候,這個命名空間就被"自動"引入了.當尋找函數printMyClassInfo的時候會在當前的上下文中進行尋找的同時,還會到以前用到過的命名空間中去尋找.
當然,通常情況下我們喜歡這樣做:
using namespace MyNameSpace;
int main()
{
MyClass instance("MyClass!\n");
printMyClassInfo(instance);
}
但是并不是在任何情況下都鼓勵using namespace這種做法的.在書中將得比較清楚:
Note that you should never use a using directive when the context is not clear (such as in header
files, modules, or libraries). The directive might change the scope of identifiers of a namespace,
so you might get different behavior than the one expected because you included or used your
code in another module. In fact, using directives in header files is really bad design.
上面這段話強調了當上下文并不明確的情況下(比如在一個頭文件,組件或者庫里面),不要使用using這種寫法,這個指令會改變命名空間標識符的作用域,這樣你就有可能引發和你預期不相同的行為,因為你會在另外一個組件中引用你的代碼或使用它.事實上,將using標識符寫在頭文件里面是一種相當不好的設計.
在這里,我看了一下c++程序設計語言這本書,發現命名空間除了像上面這樣聲明以外,還可以像類一樣這樣來寫:
在命名空間中這樣定義
void printMyClassInfo(MyClass &);
然后在外面寫函數的主體
void MyNameSpace::printMyClassInfo(MyClass &instance)
{
std::cout<<instance.getClassInfo();
}
好了,寫了這么多,再來看看剛才留下來的那個問題.
其實很簡單,一個函數如果傳遞的是值,那么就會在內存中產生一個一模一樣的"復本",而那個字符指針也會被復制一次.當傳送的值超過它的作用域的時候 ,就會被釋放掉,而被復制的"本體"在程序運行結束之后,又會被"釋放一次".這樣在運行的時候,它會提示你這樣的錯誤:
*** glibc detected *** double free or corruption (fasttop): 0x0804a008 ***
在我們的MyClassl類的析構中,我們有一個輸出,所以這里就輸出了兩次:
free classinfofree classinfo
posted on 2007-06-14 22:06
littlegai 閱讀(283)
評論(0) 編輯 收藏 引用 所屬分類:
我的讀書筆記