Solved

Multiple constructors

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hibernate methods 2 77
Doc'in system (example?) BA 7 115
My project did see openJDK that I installed. What could be the problem 7 151
Path of Workbook 3 86
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 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