2010/10/28

[C++] Forward Declaration for Circular Dependency Problem

0. Keywords

Cyclic Dependency, Circular Dependency, Cross Dependency, Forward Declaration.

1. Cyclic Dependency (Circular Dependency, Cross Dependency) Problem

Two classes depending on each other would cause Cyclic Dependency (Circular Dependency, Cross Dependency). For example, class A (in A.h) is associated with class B in (B.h) and vice versa.

main.cpp

#include "A.h"
#include "B.h"

int main() {return 0;}

 

A.h

#ifndef _A_H_
#define _A_H_

#include "B.h"

class A{
    B* b; // A depends on B
};

#endif

 

B.h

#ifndef _B_H_
#define _B_H_

#include "A.h"

class B{
    A* a;  // B depends on A

};

#endif

Let's see what would happen by tracing the code at main.cpp. 

At frist, the statement #include "A.h" is placed. Preprocessor go to A.h, finding out that _A_H_ is not yet defined (#ifndef _A_H_), so preprocessor define _A_H_ (#define _A_H_ ). After that, it encounter #include "B.h".

Then preprocessor go to B.h, finding out that _B_H_ is not yet defined (#ifndef _B_H_), so preprocessor define _B_H_ (#define _B_H_ ). After that, it encounter #include "A.h". However, A.h will not be included in B.h again because _A_H_ is already defined now.

Note that class B have no idea about A. Therefore, in the the declararion of class B in B.H, declararing A* a; will cause error since A is not a class, a struct, or a primitive type in the view of class B. The error message would be something like:

error C4430: miss type - C++ does not support default-int.

 

2. solution

The Circular Dependency Problem can be solved by Forward Declaration of class A such that class B would know A is a class.

B.h

#ifndef _B_H_
#define _B_H_

#include "A.h"

class A;

class B{
    A* a;
};

#endif

 

3. Incomplete Type Problem for Forward Declaration

Before a type's  definition is seen, this type is called incomplete type. The statement

class A; in B.h in the code above tells that A is a class without  definition. In this case, A is an incomplete type.

It is not possible use an incomplete type to define a variable or class member. An incomplete type may be used to define only pointers or references to the type or to declare (but not define) functions that use the type as a paremeter or return type.[1]

In the following code, we observe that the statement a->doSomething(); in B.h causes a error because class B knows A is a class only and the definition of doSomething(); of class A has not yet been seen.

Moreover, after we delete a->doSomething(); in B.h, no error messages would show. But why b->doSomething(); in A.h works fine? It because the class A know everything about class B, including the definition of B. The class B is a complete type in the view of class A.

main.cpp

#include "A.h"
#include "B.h"

int main() {return 0;}

A.h

#ifndef _A_H_
#define _A_H_

#include "B.h"

class B;

class A{
public:   
    B* b; 
    void doSomething(){}
    A(){
        b->doSomething(); // ok
    }

};

#endif

B.h

#ifndef _B_H_
#define _B_H_

#include "A.h"

class A;

class B{
public:
    A* a;
    void doSomething(){}
    B(){
        a->doSomething(); // error
    }
};

#endif

Similarly, we know the fact that an incomplete type can not define a variable or class member. Thus ,the following code causes a error when defining an incomplete class member (The statement A a; in class B)

A.h

#ifndef _A_H_
#define _A_H_

#include "B.h"

class B;

class A{ 
    B b; // ok
};

#endif

B.h

#ifndef _B_H_
#define _B_H_

#include "A.h"

class A;

class B{
    A a; // error
};

#endif

Reference

[1] By Stanley B. Lippman, Josée Lajoie, Barbara E. Moo, "C++ Primer, Fourth Edition", Publisher: Addison Wesley Professional, Pub Date: February 14, 2005, ISBN: 0-201-72148-1.

[2] Forward declaration From Wikipedia.

[3] Circular dependency From Wikipedia.

More posts regarding [C++]:

  1. implicit constructor
  2. 使用Pointer to pointer或Reference to pointer,改變pointer的address
  3. Dynamic Memory Allocation in C and C++
  4. [C++] Call by value、Call by pointer、Call by reference
  5. (C++)virtual與pure virtual

2010/10/10

「雲端防毒軟體Cloud Antivirus」 - Panda Security

0. 摘要 Abstract

本文將介紹雲端運算Cloud Computing概念以及雲端防毒Cloud Antivirus的優點,並且介紹相關雲端防毒軟體。

關鍵字:雲端運算雲端計算雲端防毒雲端運算技術漫步在雲端雲端計算雲端運算概念、雲端技術雲端運算wiki雲端服務三義漫步雲端雲端運算應用

1. 雲端運算 Cloud Computing

雲端運算,是一種基於互聯網的計算新方式,通過互聯網的服務為個人和企業用戶提供按需即取的計算。由於資源是在互聯網上,而在電腦流程圖中,網際網路常以一個雲狀圖案來表示,因此可以形象地類比為雲端。 雲端運算的資源是動態易擴展而且虛擬化的,通過互聯網提供。終端用戶不需要了解「雲」中基礎設施的細節,不必具有相應的專業知識,也無需直接進行控制。(以上:wiki)

Cloud_computing

雲端上的伺服器為提供使用者各式各樣的服務(圖:wiki)

2. 雲端防毒 Cloud Antivirus

看完以上wiki介紹的「雲端運算」概念後,接下來版主將介紹「雲端防毒」概念以及相關產品。

至於所謂的「雲端防毒」就是透過雲端上的伺服器來幫你做掃毒、偵測、保護的功能。使用者不必再更新病毒碼,因為所有的防毒工作伺服器都幫你做好了。這樣的好處有:

1. 零時差的保護:在傳統的防毒程式中,病毒碼發布的時間往往相當的慢,從發現疑似的病毒,確認病毒,到發佈更新病毒碼至少也要1小時。但是這時候你的電腦可能已經被感染了。使用「雲端防毒」技術可以讓遠端伺服器集群體智慧幫你掃描任一個檔案,你得到的服務永遠的是最新的,分秒不差。你再也不必為了更新病毒碼跟時間賽跑。

2. 減少使用者運算量:由於「雲端防毒」將病毒偵測的計算過程移到遠端大量且快速的伺服器,使用者電腦不必再佔用處理器與記憶體資源做這些沒有工作產值的事情,你的電腦可以專心為你處理更重要的事情。

總結來說:「雲端防毒」提供了以秒計算的即時防護,並且提供集群體力量的厚實保護。

目前支援「雲端防毒」的防毒軟體有:賽門鐵克Symantec的諾頓Norton 2010、趨勢科技的PC-cillin 2011、貓熊Panda Security。

2010/10/09

退伍倒數計時器、役期倒數計時器(程式版/免上網)

Tags:退伍日期計時器,退伍日期計時器,退伍倒數計時器,退伍日期倒數計時器,退伍倒數計時,倒數計時器,當兵倒數計時器,倒數計時器下載,倒數計時器語法,桌面倒數計時器,倒數計時器軟體,msn倒數計時器,役期倒數計時器,國軍倒數計時器,海軍倒數計時器,空軍倒數計時器,陸軍倒數計時器,海陸倒數計時器,替代役倒數計時器

當兵是很多人的共同經驗,雖然現在役期大幅減少,但是退伍的日期還是感覺遙遙無期,借由退伍日期倒數計時器的幫助,可以讓你精確掌握目前的進度。你只要在退伍日期倒數計時器的程式畫面上選擇好入伍日期、役期、和折抵天數即可顯示退伍日期、入伍天數、離退伍天數、已經過了幾%等資訊。

其中,入伍日期只要選擇一次即可,退伍日期倒數計時器就會記錄下這天,以方便您下次開啟退伍日期倒數計時器後,自動讀取。

本程式為免費軟體,歡迎你自由使用與推廣。有任何建議,盡請不吝指教。

退伍倒數計時器(Military Online Counter)

退伍倒數計時器 - 程式畫面

軟體性質: 免費軟體
更新日期: 2010/10/09
最近版本: 1.0
作業系統: Windows
語言界面: 中文
原創作者: 藏經閣 http://csie-tw.blogspot.com
產品頁面: http://csie-tw.blogspot.com/2010/10/military-online-counter.html
下載點1 (訊6空間,169 KB)
下載點2 (Google空間,169 KB)


PS:使用本程式必須先安裝.NET Framework,請按此觀看更多資訊。

退伍倒數計時器(Military Online Counter)
2Ting Anonymous Mail (匿名寄信)
2Ting Screen Capture(螢幕擷取)
2Ting MouseClick 2.0(滑鼠連點程式)
2Ting 身分證字號製造機
韓國身分證字號產生器

何謂博士? (The illustrated guide to a Ph.D.)

Imagine a circle that contains all of human knowledge:

想向一下圓圈裡面包含人類已知的所有知識:

   csie-tw.blogspot.com

By the time you finish elementary school, you know a little:

當你小學畢業時,你懂了一些些:

 csie-tw.blogspot.com (1)

By the time you finish high school, you know a bit more:

當你中學畢業時,你懂更多了:

csie-tw.blogspot.com (2)

With a bachelor's degree, you gain a specialty:

當你大學畢業,你得到某個方面的專業知識:

csie-tw.blogspot.com (3)

A master's degree deepens that specialty:

當你碩士畢業,你更增強了這方面的知識:

csie-tw.blogspot.com (4)

Reading research papers takes you to the edge of human knowledge:

接著,你就由閱讀大量研究論文,使你這方面的專業已經達到人類知識的極限。

csie-tw.blogspot.com (5)

Once you're at the boundary, you focus:

你持續專注在這邊界上:

csie-tw.blogspot.com (6)

You push at the boundary for a few years:

你花了好幾年的功夫持續專研:

csie-tw.blogspot.com (7)

Until one day, the boundary gives way:

直到有一天...你突破了人類知識的極限:

csie-tw.blogspot.com (8)

And, that dent you've made is called a Ph.D.:

所謂的博士就是製造這凸痕的人:

csie-tw.blogspot.com (9)

Of course, the world looks different to you now:

當然,這世界對你來說,有了新的面貌:

csie-tw.blogspot.com (10)

So, don't forget the bigger picture:

但不要忘記原本的那張圖:csie-tw.blogspot.com (11)

Keep pushing.

持續往前邁進吧!

Why genetics research? 為何要做基因研究?

If you zoom in on the boundary of human knowledge in the direction of genetics, there's something just outside humanity's reach:

如果你放大剛剛那個圖,你可以看到圓圈的外面,出現人類知識上的新里程碑:

csie-tw.blogspot.com (12)

My wife and I chose to start funding these graduate students after we learned that our son has a rare, fatal genetic disorder.

在我跟我太太發現我們的兒子得了罕見且致命的遺傳疾病時,我們開始成立基金會,用來資助研究生做研究。

It may be too late for my son, but it's not too late for other children.

或許,對我兒子來說,為時已晚,但是對其他孩子來說,卻不太晚。

Even one child suffering is one child too many. The only way to end this kind of suffering is science.

有太多小孩承受這種痛苦,唯一的解決辦法就是科學。

And, the best way to do science is through graduate students.

研究生就是做科學研究最佳人選。

原文作者:Matt Might

原文文章:http://matt.might.net/articles/phd-school-in-pictures/

譯者:csie-tw

翻譯文章:http://csie-tw.blogspot.com/2009/10/bloggerrandom-post.html

This work is licensed under the Creative Commons Attribution-NonCommercial 2.5 License.  That means you can share, copy, modify and reproduce this work as long as you attribute the original work to me and link back to it as outlined above.

本文使用姓名標示-非商業性 2.5授權。也就是,只要你明確標示本文來源、並且使用反向連結至原文,你可以分享、複製、修改、重製本文。

 

csie-tw 牛津大學博士生畢業服

Buddhism and Software Developer

In today's fast-paced society, we are often surrounded by work, goals, and external pressures. However, the wisdom found in Buddhism off...