Solved

Multiple constructors

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

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

626 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