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

rbhargawFounderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
rbhargawFounderAuthor 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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

rbhargawFounderAuthor 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
rbhargawFounderAuthor 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.