Solved

Multiple constructors

Posted on 2000-04-03
1
183 Views
Last Modified: 2013-11-20
I have an object which has 2 constructors. One is a default and the other one accepts parameters. The default constructor calls the other one with the appropriate parametrs. The problem is that after calling the other constructors, which initializes some member data items, the member data items get overwritten by some junk values, as if they where not initialized in the first place.
Below is a code segment:

CAirportFileBuilder::CAirportFileBuilder()
{
      // Get the document
      CTheDoc      *pDoc = (CTheDoc*)((CFrameWnd*) AfxGetMainWnd())->GetActiveDocument();

      CString strName, strIdent;
      pDoc->GetAirportName (strName);
      pDoc->GetAirportIdent (strIdent);
      CAirportFileBuilder ((char*)((LPCTSTR) strName), (char*)((LPCTSTR) strIdent));
}


CAirportFileBuilder::CAirportFileBuilder (char *szName, char *sIdent)
{
      // Set the file version
      m_Hdr.iFileVersion = AIRPORT_FILE_VERSION;

      // Set the airport name
      if (szName)
      {
            strncpy (m_Hdr.szName, szName, NAME_LENGTH-1);
      }
      else
      {
            strncpy (m_Hdr.szName, "Airport", NAME_LENGTH-1);
      }
//....................... more initializations
}

Please advise on how to make this chain call to do what I want it to do.
Thank you.
0
Comment
Question by:Zainal062797
1 Comment
 
LVL 4

Accepted Solution

by:
abancroft earned 50 total points
ID: 2681443
>> CAirportFileBuilder ((char*)((LPCTSTR) strName), (char*)((LPCTSTR) strIdent));
Calling the constructor like this creates a temporary CAirportFileBuilder object which has a lifetime of the expression in which it is called - in this case, there is no expression so the temporary is destroyed very quickly.

This is not the same as calling the construction function.

What you need to do is define a function that will initialise the object. e.g.
class CAirportFileBuilder
{
public:
  CAirportFileBuilder();
  CAirportFileBuilder (LPCSTR szName, LPCSTR sIdent);

protected:
  void CommonConstruct(LPCSTR szName, LPCSTR sIdent);
};

CAirportFileBuilder::CAirportFileBuilder()
{
  // Get the document
  CTheDoc *pDoc = (CTheDoc*)((CFrameWnd*) AfxGetMainWnd())->GetActiveDocument();

  CString strName, strIdent;
  pDoc->GetAirportName (strName);
  pDoc->GetAirportIdent (strIdent);
  CommonConstruct(strName, strIdent);
}

CAirportFileBuilder::CAirportFileBuilder (LPCSTR szName, LPCSTR sIdent)
{
  CommonConstruct(strName, strIdent);
}

CAirportFileBuilder::CommonConstruct(LPCSTR szName, LPCSTR sIdent)
{
  // Initialise here....
}
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 119
while loop over for loop 7 101
viewing source code from eclipse 13 104
Modbus - whats the maximum I can store in one register? 4 79
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question