http://www.codeproject.com/KB/archit...ling_eg_1.aspx
Hehe, nói là dịch cho oai thôi chứ thực ra bài này chỉ là nêu lên một cách tóm tắt cái bài dài loằng ngoằng bằng tiếng anh ở trên thui...
A. Inline coding:
Ex:
C++ Code:
#ifndef _H_EX #define _H_EX #include <iostream> using namespace std; class Ex { public: Ex() { i = 0; iCount++; } void print() { } void pCount(); public: int i; static int iCount; }; _declspec(selectany) int Ex::iCount = 0; inline void Ex::pCount() { } static int g_Int = 5; static void ePrint() { } #endif //_H_EX
Đây là cách code class,biến toàn bộ (global variable), hàm toàn cục (global function), ... chỉ trong một header file (*.h). Cách code này được khá nhiều người sử dụng (có tui, và cả C# nữa)
Lưu ý:
+ Tất cả các global function đều phải inline (static cũng được)
+ Tất cả các global variable đều phải _declspec(selectany) (static cũng được)
+ Nếu class có sử dụng biến static thì phải add thêm _declspec(selectany) trước (kô biết cái này chỉ có MSVC hỗ trợ hay tất cả các compiler đều hỗ trợ) vì nếu không khi nhiều file *.cpp cùng include file header này thì sẽ gặp một link error giống như : "Object already defined or declared" (đối tượng đã được khai báo hoặc có body rùi) ...
+ Nếu class đã khai báo prototype của một function trong class thì nếu muốn khai báo hàm đó ở ngoài mà không bị lỗi "Object already defined or declared" thì phải xài inline (kô xài static được đâu)
+ Definition of global functions require the inline declaration. If the function is recursive (or is part of a recursion mechanism) or has too long a body, the compiler will not expand the body in the expression you call it, but in any case will place an ordinary function call. In any case, the "inline" here, plays the same role of the selectany played above. In fact, also member functions are inline, but for members defined in that way, the inline is implicit. (khúc này hiểu nhưng kô biết giải thích làm sao)
Bất lợi:
+ Việc đọc code dạng này sẽ khá bất lợi, đặc biệt khi code quá dài hoặc rắc rối (giống như class này sử dụng class kia A USE B AND B USE A) [seo mình kô thấy vậy ta]
+ Việc sử dụng code cũng sẽ gặp rắc rối trong trường hợp A USE B AND B USE A
Vậy thì hãy tiến tới cách code thứ hai...
B. Near Line Coding
Chôm ví dụ của nó![]()
C++ Code:
//********************** // file "myclass.h" // include used types declarations #include "MyBase.h" #include "MyInterface.h" //declare CMyClass class CMyClass: public CMyBase, public IMyInterface //if any { //data members CSometypeA _memberVariable; //static member CSometypeB s_staticMember; //MyClass functions inline returntype DoSomething( parameters ); //CMyBase overrides inline virtual returntype DoOverride ( parameters ); //IMyInterface implementation inline virtual returntype DoImplement( parameters) }; //reference static members (if externally needed) extern CSometypeB CMyClass::s_staticMember; //instantiate global objects extern CSometypeC g_glbalObject; //declare some global function inline returntype GlobalFunction (parameters); //now include the definitions #include "myclass.hpp"
C++ Code:
//************************** // file "myclass.hpp" #pragma once // include used types declarations not already in Myclass.h // define CMyClass //MyClass functions imline returntype CMyClass::DoSomething( parameters ) { //function body goes here } //CMyBase overrides inline returntype CMyClass::DoOverride ( parameters ) { //function body goes here } //IMyInterface implementation inline returntype CMyClass::DoImplement( parameters) { function body goes here } //instantiate static members _declspec(selectany) CSometypeB CMyClass::s_staticMember; //instantiate global objects _declspec(selectany) CSometypeC g_glbalObject; //declare some global function inline returntype GlobalFunction (parameters) { //function body }
Thoạt nhìn cách code này giống như cách code nhiều LTV khác thường làm (viết signature ở header file rồi viết body ở cpp file) nhưng thực ra không phải vậy. Việc include file hpp ở cuối file h sẽ làm cho việc add file h sẽ kèm theo luôn file hpp, và điều này tạo nên sự thuân tiện trong một số trường hợp.
Lưu ý:
+ Bởi vì chúng ta vẫn chưa add cpp file nên vẫn cần static , inline và _declspec(selectany)
+ Trong file h, khi khai báo biến static của class hay biến toàn cục chưa dùng static và _declspec(selectany) vội mà phải dùng extern rồi mới dùng static và _declspec(selectany) ở file hpp sau
+ Còn lại giông với A. Inline Coding
Vậy điều gì thực sự khác biệt ở B. Near Line Coding. Có lẽ là việc dễ dàng cho việc đọc code, thuận tiện trong trường hợp A USE B AND B USE A và dễ dàng trong việc tái sử dụng (chỉ cần add file h là sẽ tự động add luôn file hpp)
C. Ofline Coding
Đây có lẽ là cách code hay sử dụng nhất của LTV, nên chắc khá quen rồi vậy mình sẽ không trình bày ở đây có gì xem thêm ở cái bài tiếng anh trên
... Thấy cái này hay và có lẽ khá hữu dụng trong một số trường hợp nên post lên đây. Mấy hôm trước đang gặp rắc rối trong môt project khá lớn, vớ được cái này mừng hết xiết. Hy vọng sẽ giúp ích cho các bạn
-----
I'm not a beginer... just a collector and a manager...
Đã được chỉnh sửa lần cuối bởi vpdp_pc : 11-04-2008 lúc 08:07 PM.