VC++ MFC class data member initialisation [modified]
-
I am learning to use VS2005 creating a VC++ MFC project to unterface to serial ports. I am in the initial stages of getting to grips with creating a serial port class. I am finding a few problems along the way with vartiable / data member initialisation. CGCSerial h-file int CharCount; int CharCount2; int CharCountThree; CString TxString; size_t InputStringLength; TCHAR * pStr; char * pOutputString; cpp-file(s) CMyDrawView::OnToolWrite2SerialPort //member function called by clicking on windows menu item { CGCSerial *pGcSp = new CGCSerial(_T("12345")); //create instance of CGCSerial class pGcSp -> GCTx; //call transmit member function delete pGcSp; //cleanup } CGCSerial::GCSerial(TxString) { //constructor CharCount2 = 0 CharCountThree = 0; } CGCSerial::~GCSerial { //destructor } CGCSerial::GCTx { InputStringLength = wcslen(TxString); //measuring length of incoming string CharCountThree = sizeof(InputStringLength); //load CharCount3 with something TempString.Format(_T("executing function CGCSerial::GCTx")); AfxMessageBox(TempString); pStr = TxString.GetBuffer(256); CharCount2 = sizeof(pStr); TxString.ReleaseBuffer; InputStringLength = wcslen(pStr); //measuring length of CharCount2 = 0x2345; // ### CharCountThree = 0x5678; //### CharCount2 = sizeof(TxString); CharCountThree = sizeof(pOutputString); pOutputString = CW2A(TxString); //convert wide string to ANSI string ... ... } My problem is that I admit this code doesn't look the most useful and is due to getting to grips with VC++ functions. The lines marked with ### don't seem to get executed! If I use the debugger, the debugger stops at these lines but upon viewing these data member's they do not get loaded with 0x2345 & 0x5678 respectively??? Is this a compiler bug?? any help would be greatly appreciated :)
modified on Tuesday, September 8, 2009 8:21 AM
-
I am learning to use VS2005 creating a VC++ MFC project to unterface to serial ports. I am in the initial stages of getting to grips with creating a serial port class. I am finding a few problems along the way with vartiable / data member initialisation. CGCSerial h-file int CharCount; int CharCount2; int CharCountThree; CString TxString; size_t InputStringLength; TCHAR * pStr; char * pOutputString; cpp-file(s) CMyDrawView::OnToolWrite2SerialPort //member function called by clicking on windows menu item { CGCSerial *pGcSp = new CGCSerial(_T("12345")); //create instance of CGCSerial class pGcSp -> GCTx; //call transmit member function delete pGcSp; //cleanup } CGCSerial::GCSerial(TxString) { //constructor CharCount2 = 0 CharCountThree = 0; } CGCSerial::~GCSerial { //destructor } CGCSerial::GCTx { InputStringLength = wcslen(TxString); //measuring length of incoming string CharCountThree = sizeof(InputStringLength); //load CharCount3 with something TempString.Format(_T("executing function CGCSerial::GCTx")); AfxMessageBox(TempString); pStr = TxString.GetBuffer(256); CharCount2 = sizeof(pStr); TxString.ReleaseBuffer; InputStringLength = wcslen(pStr); //measuring length of CharCount2 = 0x2345; // ### CharCountThree = 0x5678; //### CharCount2 = sizeof(TxString); CharCountThree = sizeof(pOutputString); pOutputString = CW2A(TxString); //convert wide string to ANSI string ... ... } My problem is that I admit this code doesn't look the most useful and is due to getting to grips with VC++ functions. The lines marked with ### don't seem to get executed! If I use the debugger, the debugger stops at these lines but upon viewing these data member's they do not get loaded with 0x2345 & 0x5678 respectively??? Is this a compiler bug?? any help would be greatly appreciated :)
modified on Tuesday, September 8, 2009 8:21 AM
GC104 wrote:
CharCount2 = 0x2345; // ### CharCountThree = 0x5678; //### CharCount2 = sizeof(TxString); CharCountThree = sizeof(pOutputString);
Maybe the compiler optimizes that to:
CharCount2 = sizeof(TxString);
CharCountThree = sizeof(pOutputString);BTW please use the
code block
to submit code snippets. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I am learning to use VS2005 creating a VC++ MFC project to unterface to serial ports. I am in the initial stages of getting to grips with creating a serial port class. I am finding a few problems along the way with vartiable / data member initialisation. CGCSerial h-file int CharCount; int CharCount2; int CharCountThree; CString TxString; size_t InputStringLength; TCHAR * pStr; char * pOutputString; cpp-file(s) CMyDrawView::OnToolWrite2SerialPort //member function called by clicking on windows menu item { CGCSerial *pGcSp = new CGCSerial(_T("12345")); //create instance of CGCSerial class pGcSp -> GCTx; //call transmit member function delete pGcSp; //cleanup } CGCSerial::GCSerial(TxString) { //constructor CharCount2 = 0 CharCountThree = 0; } CGCSerial::~GCSerial { //destructor } CGCSerial::GCTx { InputStringLength = wcslen(TxString); //measuring length of incoming string CharCountThree = sizeof(InputStringLength); //load CharCount3 with something TempString.Format(_T("executing function CGCSerial::GCTx")); AfxMessageBox(TempString); pStr = TxString.GetBuffer(256); CharCount2 = sizeof(pStr); TxString.ReleaseBuffer; InputStringLength = wcslen(pStr); //measuring length of CharCount2 = 0x2345; // ### CharCountThree = 0x5678; //### CharCount2 = sizeof(TxString); CharCountThree = sizeof(pOutputString); pOutputString = CW2A(TxString); //convert wide string to ANSI string ... ... } My problem is that I admit this code doesn't look the most useful and is due to getting to grips with VC++ functions. The lines marked with ### don't seem to get executed! If I use the debugger, the debugger stops at these lines but upon viewing these data member's they do not get loaded with 0x2345 & 0x5678 respectively??? Is this a compiler bug?? any help would be greatly appreciated :)
modified on Tuesday, September 8, 2009 8:21 AM
Could it be that you are debugging a release build?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <
-
Could it be that you are debugging a release build?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <
Hi all, Thanks for responses. It looks like CPallini nailed it - the compiler doing its job! I have since added more data members to capture the results of the various functions I used on the original code. However this time, the debugger runs through eacdh line and loads each variable with the value I was expected many thanks Geoff :)