Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Multiple constructors

Posted on 2000-04-03
1
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
abancroft earned 200 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

730 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