Solved

Multiple constructors

Posted on 2000-04-03
1
178 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Complete beginner needs help making a cron job 9 104
string initialization in java 11 103
Problem to event 3 50
Thin secure Windows 10 5 49
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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 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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now