1) 使用windows 頭文件
API 頭文件允許32位和64位應(yīng)用程序,包含了ANSI版本和UNICODE版本的聲明。
如果安裝更新SDK的話,那么就可能有頭文件的多個(gè)版本在機(jī)器上面。
一些函數(shù)的使用可能會(huì)通過使用條件編譯代碼依賴于某個(gè)特定版本的操作系統(tǒng),為了編譯成功,你得定義比較合適的macro.頭文件使用宏來(lái)指示哪個(gè)版本系統(tǒng)支持編程元素。
http://msdn2.microsoft.com/en-us/library/aa383745(VS.85).aspx2) 初始化類中的成員模板
i) 使用非模板的方式
template <typename Argtype>
class Option
{
public:
Option( void (*func_name)(Argtype), Argtype Arg1 )
: MenuFunction<Argtype>( (*func_name)(Argtype), Argtype Arg1 )
{
}
private:
MenuFunction<Argtype> function;
};
template <typename Argtype>
Option<Argtype> makeOption(void (*func_name)(Argtype), Argtype Arg1 )
{
return Option<Argtype>(func_name, Arg1);
}ii) 使用多態(tài)
class Option
{
private:
class FunctionBase
{
public:
virtual ~FunctionBase() {}
virtual void call() = 0;
};
template <typename Argtype>
class Function : public FunctionBase
{
public:
Function(void (*func_name)(Argtype), Argtype arg) :
m_func(func_name, arg)
{
}
void call()
{
m_func(); // or whatever
}
private:
MenuFunction<Argtype> m_func;
};public:
template<typename Argtype> Option( void (*func_name)(Argtype), Argtype Arg1 )
{
// of course, this means you need a destructor, copy constructor, and assignment operator
// function->call() would invoke the function
function = new Function<Argtype>(func_name, Arg1);}
FunctionBase * function;};
3 大小寫字符串比較大小(考慮區(qū)域性語(yǔ)言的問題)
#include <iostream>
#include<algorithm>
#include<functional>
#include<boost/bind.hpp>
#include<string>
#include<locale>
struct CaseSensitiveString
{
public:
bool operator()(const std::string & lhs,const std::string & rhs)
{
std::string lhs_lower;
std::string rhs_lower;
std::transform(lhs.begin(),lhs.end(),std::back_inserter(lhs_lower),bind(std::tolower<char>,_1,_loc));
std::transform(rhs.begin(),rhs.end(),std::back_inserter(rhs_lower),bind(std::tolower<char>,_1,_loc));
return lhs_lower < rhs_lower;
}
CaseSensitiveString(const std::locale & loc):_loc(loc){}
private:
std::locale _loc;
};
詳細(xì)內(nèi)容見:
http://learningcppisfun.blogspot.com/2008/04/case-insensitive-string-comparison.html4 找不到msctf.h問題
在用DX自帶的dxut做界面程序的時(shí)候,整個(gè)程序編制下來(lái)就出現(xiàn)了這個(gè)錯(cuò)誤
fatal error C1083: Cannot open include file: 'msctf.h': No such file or directory
很詭異的,在dxsdk里面也找不到,想了很久,才發(fā)現(xiàn)自己沒有安裝platform sdk.因?yàn)閣in32程序之類的,最好都要安裝這些sdk之類的。具體的信息可以在這里得到
http://www.gamedev.net/community/forums/topic.asp?topic_id=4813585 重載 , 覆蓋,隱藏
重載與覆蓋有以下的區(qū)別:
重載:同一類,相同函數(shù)名,不同函數(shù)參數(shù),不一定要有virtual 關(guān)鍵字
覆蓋:子類和父類,相同函數(shù)名, 相同函數(shù)參數(shù),一定要有virtual 關(guān)鍵字
隱藏:1)如果派生類的函數(shù)名與基類的函數(shù)名相同,但是參數(shù)不同,不論有無(wú)virtual關(guān)鍵字,基類的函數(shù)將被隱藏(與重載區(qū)別開來(lái))
2)如果派生類的函數(shù)名與基類的函數(shù)名相同,并且參數(shù)相同,但是基類沒有virtual關(guān)鍵字,基類的函數(shù)將被隱藏(與覆蓋區(qū)別開來(lái))
6 快速加載文件
在游戲里面,一般對(duì)從硬盤或者DVD加載資源要求比較高的,一般采用這樣的方法:
for(int i = 0; < NumBlocks; i++)
{
// VirtualAlloc() creates storage that is page aligned
// and so is disk sector aligned
blocks[i] = static_cast<char *>
(VirtualAlloc(0, BlockSize, MEM_COMMIT, PAGE_READWRITE));
ZeroMemory(&overlapped[i], sizeof(OVERLAPPED));
overlapped[i].hEvent = CreateEvent(0, false, false, 0);
}
HANDLE hFile = CreateFile(FileName, GENERIC_READ, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING |
FILE_FLAG_OVERLAPPED | FILE_FLAG_SEQUENTIAL_SCAN, 0);
int iWriterPos = 0;
int iReaderPos = 0;
int iIOPos = 0;
int iPos = 0;
do
{
while(iWriterPos - iReaderPos != NumBlocks && iIOPos < FileSize)
{
overlapped[iWriterPos & NumBlocksMask].Offset = iIOPos;
int iLeft = FileSize - iIOPos;
int iBytesToRead = iLeft > BlockSize ? BlockSize: iLeft;
const int iMaskedWriterPos = iWriterPos & NumBlocksMask;
ReadFile(hFile, blocks[iMaskedWriterPos], iBytesToRead, 0,
&overlapped[iMaskedWriterPos]);
iWriterPos++;
iIOPos += iBytesToRead;
}
const int iMaskedReaderPos = iReaderPos & NumBlocksMask;
WaitForSingleObject(overlapped[iMaskedReaderPos].hEvent, INFINITE);
int iLeft = FileSize - iPos;
int iBytesToRead = iLeft > BlockSize ? BlockSize: iLeft;
memcpy(&g_buffer[iPos], blocks[iMaskedReaderPos], iBytesToRead);
iReaderPos++;
iPos += iBytesToRead;
}
while(iPos < FileSize);
CloseHandle(hFile);
for(int i = 0; i < NumBlocks; i++)
{
VirtualFree(blocks[i], BlockSize, MEM_COMMIT);
CloseHandle(overlapped[i].hEvent);
}
char* s vs char s[]
char s1[] = "abcd";// 定義一個(gè)未指定長(zhǎng)度的char型數(shù)組,并使用字符串"abcd"將之初始化
char *s2 = "abcd";// 定義一個(gè)char型指針,并將其指向字符串"abcd",該字串位于靜態(tài)存儲(chǔ)區(qū)
s1[0] = 'm';// 無(wú)編譯期、運(yùn)行期錯(cuò)誤
s2[0] = 'm';// 無(wú)編譯器錯(cuò)誤,但運(yùn)行期試圖修改靜態(tài)內(nèi)存,所以發(fā)生運(yùn)行期錯(cuò)誤
char s*只是被賦予了一個(gè)指針,char s[]是在棧中重新開辟了空間,可以在程序中寫,而不引起程序崩潰。
所以相比較而言,使用字符串?dāng)?shù)組要比字符指針要安全的多,要慎用char*s 和char s[].
7 can not find MSVCR80.dll
在安裝了vc2005之后,發(fā)現(xiàn)錯(cuò)誤報(bào)告說(shuō)MSVCR80.dll,以為又要重新安裝vc2005了,但是在網(wǎng)絡(luò)上面搜索到另外一個(gè)例子說(shuō),其實(shí)可以不用安裝vc2005,直接改變配置就好了,于是就有這個(gè)了:
http://blogs.msdn.com/seshadripv/archive/2005/10/30/486985.aspx
http://www.codeguru.com/forum/showthread.php?t=439964
工程架構(gòu):
新建一個(gè)空白的 solution.
然后在新建的solution上面添加vcproject.
并且也可以在子空白solution上面添加vcproject.
1> ]
1>正在編譯資源...
1>正在鏈接...
1>LINK : warning LNK4075: 忽略“/INCREMENTAL”(由于“/OPT:ICF”規(guī)范)
1>fatal error C1900: Il mismatch between 'P1' version '20060201' and 'P2' version '20050411'
1>LINK : fatal error LNK1257: 代碼生成失敗
1>生成日志保存在“file://e:\demo-work\LocalVersionTianJi\_out\DragoonApp\Release\BuildLog.htm”
1>DragoonApp - 1 個(gè)錯(cuò)誤,5382 個(gè)警告
========== 全部重新生成: 0 已成功, 1 已失敗, 0 已跳過 ==========
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1512436&SiteID=1
http://forum.codecall.net/c-c/6244-fatal-error-c1900-il-mismatch-between-p1-version-20060201-p2-version-2005-a.html
http://www.codeguru.com/forum/archive/index.php/t-144030.html
timeGetTime: 頭文件 mmsystem.h,庫(kù)文件 winmm.lib
獲得當(dāng)前窗口的句柄。
HWND hwnd=::GetForegroundWindow();
hwnd就保存了當(dāng)前系統(tǒng)的最頂層窗口的句柄
GetSafehWnd 取你程序所在窗口類的句柄
GetActiveWindow 取當(dāng)前活動(dòng)窗口句柄
AfxGetMainWnd 取主窗口句柄
GetForegroundWindow 取前臺(tái)窗口句柄
FindWindow
EnumWindow
改變窗口屬性:
SetWindowLong
SetClassLong.
strcpy strncpy memcpy.
strcpy:按照msdn的話說(shuō)是:No overflow checking is performed when strings are copied or appended,即沒有嚴(yán)格的長(zhǎng)度檢查,所以即使是溢出也無(wú)法被檢查出來(lái),以及The behavior of strcpy is undefined if the source and destination strings overlap.
strncpy:雖然加入了size這個(gè)來(lái)限制,但是這個(gè)size小于或者等于字符長(zhǎng)度的話,那么該信息是不被加上字符串結(jié)束符的.并且仍舊存在跟strcpy一樣的問題, The behavior of strncpy is undefined if the source and destination strings overlap
memcpy:具體的用法跟strncpy類似,也加入了size的成分在里面,但是卻比strncpy好用得多.
If the source and destination overlap, this function does not ensure that the original source characters in the overlapping region are copied before being overwritten.
Use memmove to handle overlapping regions.
顯然它能夠處理重疊的部分,安全可靠.
并且:
The first argument, dest, must be large enough to hold count characters of src; otherwise, a buffer overrun can occur.
上次遇到的問題是我將一串漢字用strcpy來(lái)拷貝到緩沖里面,結(jié)果發(fā)現(xiàn)出現(xiàn)了亂碼.
strncpy, strcpy還是建議少用,換用memcpy+memmove(如果存在重疊的情況)吧:)
未完待續(xù).