He leído mal su pregunta y respondido cuando es apropiado usar funciones estáticas.
Se refiere a las funciones estáticas de los miembros. he aquí un ejemplo de cuándo utilizar una función miembro estática - para envolver una llamada rosca dentro de una clase, de manera que el hilo tiene acceso a su clase ...:
static unsigned WINAPI ArchiveAgent::LogMsgPump(PVOID pData)
{
ArchiveAgent* pSmith = reinterpret_cast<ArchiveAgent*>(pData);
if(pSmith)
pSmith->LogMsgPump();
else
return -1;
return 0;
}
unsigned WINAPI ArchiveAgent::LogMsgPump()
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
// ....
CoUninitialize();
return 0;
}
Aquí fue mi respuesta para funciones estáticas llanura de edad .. Uso funciones estáticas donde no tiene sentido que esa función pertenezca a una clase.
Generalmente tiendo a agregar estas funciones a un espacio de nombres personalizado. El siguiente ejemplo de función estática es parte de un espacio de nombres que llamo shellutils:
static HRESULT CreateFolder(CString & sPath)
{
// create the destination folder if it doesn't already exist
HRESULT hr = S_OK;
DWORD dwError = 0;
if(sPath.GetLength() == 0 || sPath.GetLength() < 2)
return E_UNEXPECTED;
if(GetFileAttributes((LPCWSTR) sPath) == INVALID_FILE_ATTRIBUTES)
{
dwError = SHCreateDirectoryEx(NULL, (LPCWSTR)sPath, NULL);
if (dwError != ERROR_SUCCESS && dwError != ERROR_FILE_EXISTS && dwError != ERROR_ALREADY_EXISTS)
hr = HRESULT_FROM_WIN32(dwError);
}
return hr;
}
¿Qué pasa con la etiqueta 'oops'? – Maxpm
Edité la pregunta porque creo que el autor solo quiere decir "OOP" u Programación Orientada a Objetos. –
http://stackoverflow.com/questions/4723143/c-static-member-functions – Mahesh