• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 995
  • Last Modified:

warning C4172: returning address of local variable or temporary , VC++

Hello,

I am receiving the following warning in the c++ code(see in the code snippet). Can anyone tell me how to remove these warning?

Thanks
const CHolding& CFClaim::List()
{
	m_OpState = k_Cancel;
	CDlgClaimSearch dlg(m_pFormView);

	if ( dlg.DoModal () == IDOK)
	{
		m_claimRefNum = m_pDocument->m_recSPClaimSearch.m_claimRefNum;
		m_OpState = k_OK;
	}
	return *this;
}

Open in new window

0
rbhargaw
Asked:
rbhargaw
  • 6
  • 3
1 Solution
 
AndyAinscowFreelance programmer / ConsultantCommented:
From a CFClaim object you return a *this.  
The return type is declared as a const CHolding&

What is the relationship between a CFClaim and a CHolding ?
0
 
rbhargawAuthor Commented:
CHolding  is used for communication between CFObject derived classes ( CFClaim in this case) and
 Main class- they will return it as a result of edit, search, etc.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Put another way - is there a direct linkage via class inheritance eg. child, grandchild...  ?

(Look at the type of object you are returning and the type of object you tell the compiler you will be returning)
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
rbhargawAuthor Commented:
will this help?

class CFClaim : public CFObject
{
 virtual const CHolding& List();
}
--------------------------------------------------------------------------------------------------
class CHolding
{
      CHolding(const CHolding& hold)
      {
            this->m_Type = hold.m_Type;
            this->m_ID = hold.m_ID;
            this->m_Code = hold.m_Code;
            this->m_OpState = hold.m_OpState;
      }
      // constructors off all Holding areas
      // thair definitions are in the correspnding Holding implementation files
      CHolding(const CFClaim& obj);
}
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
There doesn't seem to be any relationship between the value returned ( a *this from the CFClaim) and the obejct type you say is being returned ( a CHolding&).  I think that is the root of your problem concerning the warning you are getting.
The compiler expects you to return a CHolding& object reference but you aren't, so some sort of compiler magic (patch up) is taking place via your CHolding(const CFClaim& obj); c'tor

You need to rethink (and recode) what you are doing to get around this warning
0
 
DanRollinsCommented:
Check the place that calls this function.  That is, the place in your program that starts the dialog box going.
Is it using something like:
    (myClaim->:List()).m_SomeMemberFn( foo );

Or better yet, don't answer that.  Just show the code that is making the call.  
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
class CClaim;
class CHolding
{
public:
    CHolding() {};
    ~CHolding() {};

    CHolding(const CClaim& claim) { AfxMessageBox(_T("Hello")); };

};

class CClaim
{
public:
    CClaim() {};
    ~CClaim() {};
    const CHolding& Holding() { return *this; };
};


BOOL CzzzApp::InitInstance()
{
    CWinApp::InitInstance();
    CClaim c;
    c.Holding();



When this runs you should see the message box being displayed - the compiler is constructing a local variable of type CHolding to perform the return from the function in CClaim (Which is equivalent to your code) - hence the warning you are getting.

That is why you are getting the warning.

You can either modify your code so the return is a correct variable so no temporary variable needs to be constructed.
OR add the following (to the header file) to just turn that particular warning off
#pragma warning(disable : 4172)
Note that it will turn off ALL instances of that warning

0
 
rbhargawAuthor Commented:
The call is like this
//////////////////////////////////////////////////////
m_Holding = m_pFClaim->List();
/////////////////////////////////////////////////

Here is the definition of m_Holding.

CHolding      m_Holding;



0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Did you see my last comment ?
Do you understand what I mean?
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
class CClaim;
class CHolding
{
public:
    CHolding() {};
    ~CHolding() {};

    CHolding(const CClaim& claim) { };

};

class CClaim
{
public:
    CClaim() {};
    ~CClaim() {};

//Look at the return values declared:
    const CHolding& Holding() { return *this; };
    const CClaim& Claim() { return *this; };
};


BOOL CzzzApp::InitInstance()
{
    CWinApp::InitInstance();
    CClaim c;
    CHolding h = c.Holding();    //  <<<------------------  A
    CHolding h2(c.Claim());      //  <<<------------------- B

A) This way gives you the warning because the Holding() MUST create a temp variable in the return
B) This way does not give you a warning because NO temp variable is involved


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now