Choix de construction / Initialisation d'une instance de classe
Bonjour,
je me pose une question :
vaut-il mieux faire :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| CWaveFile::CWaveFile()
{
//Init tout à Zéro
m_lpfile = NULL;
m_hwo = NULL;
m_dwDurationTime = 0;
m_hEventDone = NULL;
ZeroMemory( &m_wfex, sizeof(WAVEFORMATEX) );
ZeroMemory( &m_whdr, sizeof(WAVEHDR) );
}
CWaveFile::~CWaveFile()
{
//arreter la diffusion en cours
Stop();
//fermer le device
Close();
//Libération mémoire et handle allouée
if( m_lpfile)
delete [] m_lpfile;
if( m_hEventDone )
CloseHandle( m_hEventDone );
}
MMRESULT CWaveFile::Open(LPCSTR szFileName, UINT uDeviceID /*=WAVE_MAPPER*/)
{
// Open and read the wave file
FILE* f = fopen(szFileName, "rb");
if(!f) // Error, file not found.
return MMSYSERR_ERROR;
// Determine the file size
fseek(f, 0, SEEK_END);
long fileLen = ftell(f);
// Rewind the pointer to the begginning so we can read it
fseek(f, 0, SEEK_SET);
// Request enough memory to store the entire file
m_lpfile = new char [fileLen];
// 'Copy' the file to memory
fread(m_lpfile, sizeof(char), fileLen, f);
// Close the file, we won't need it anymore
fclose(f);
// Fill WAVEFORMATEX with the data from the file
m_wfex.wFormatTag = *((WORD* )(m_lpfile + OFFSET_FORMATTAG ));
m_wfex.nChannels = *((WORD* )(m_lpfile + OFFSET_CHANNELS ));
m_wfex.nSamplesPerSec = *((DWORD*)(m_lpfile + OFFSET_SAMPLESPERSEC ));
m_wfex.nAvgBytesPerSec = *((DWORD*)(m_lpfile + OFFSET_AVGBYTESPERSEC));
m_wfex.nBlockAlign = *((WORD* )(m_lpfile + OFFSET_BLOCKALIGN ));
m_wfex.wBitsPerSample = *((WORD* )(m_lpfile + OFFSET_BITSPERSAMPLE ));
m_wfex.cbSize = 0;
// Fill the WAVEHDR structure
m_whdr.lpData = m_lpfile+HEADER_SIZE;
m_whdr.dwBufferLength = fileLen-HEADER_SIZE;
//Durée prévu (en millisecondes)
m_dwDurationTime = (float)m_whdr.dwBufferLength / m_wfex.nSamplesPerSec / (m_wfex.wBitsPerSample/8) * 1000;
// Open and configure the device
return waveOutOpen(&m_hwo, uDeviceID, &m_wfex, (DWORD)WaveOutProc, (DWORD)this, CALLBACK_FUNCTION);
} |
Ou alors prendre le code "initialisant" de CWaveFile::Open pour le mettre dans le constructeur ?
Pour moi la raison qui fait que ce n'est pas dans le constructeur, c'est que le si le fopen() foire mon constructeur échoue.
Donc j'ai préféré faire un constructeur par défaut, et tester ensuite le CWaveFile::Open()
Qu'en pensez-vous ?