Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. VC++ MFC class data member initialisation [modified]

VC++ MFC class data member initialisation [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpdebuggingquestionlearning
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GC104
    wrote on last edited by
    #1

    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

    C C 2 Replies Last reply
    0
    • G GC104

      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

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      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]

      1 Reply Last reply
      0
      • G GC104

        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

        C Offline
        C Offline
        Code o mat
        wrote on last edited by
        #3

        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. <

        G 1 Reply Last reply
        0
        • C Code o mat

          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. <

          G Offline
          G Offline
          GC104
          wrote on last edited by
          #4

          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 :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups