Link to home
Start Free TrialLog in
Avatar of rbhargaw
rbhargawFlag for United States of America

asked on

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

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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 ?
Avatar of rbhargaw

ASKER

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.
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)
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);
}
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
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.  
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

The call is like this
//////////////////////////////////////////////////////
m_Holding = m_pFClaim->List();
/////////////////////////////////////////////////

Here is the definition of m_Holding.

CHolding      m_Holding;



Did you see my last comment ?
Do you understand what I mean?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial