pass by reference error
-
Hi I am trying to pass by reference a variable. Below is the code. In the class def I have
void OnLoseFocusDate( Date &jdtDate);
and in the src file I havevoid CEQSLowerInterestLeg::OnLoseFocusDate( Date &jdtDate) { if( jdtDate != Util::grdtNull) { Date adjust_date( jdtDate ); const CCalendar *calendar = CHolidayMgr::GetInstance().GetEquityLegPaymentHoliday(); if (calendar) { adjust_date = calendar->BusinessDayIf(adjust_date, (EnBusDayConvention)m_jcmBDC.GetSelectedObjectID(), &ToolUtils::PromptForBusinessDay); jdtDate = adjust_date; } } } // end of OnLoseFocusDate
with the function call beingOnLoseFocusDate(m_jdtTerminationDate.GetValue());
where GetValue() is defined asDate NewDate::GetValue(void) { if (::IsWindow(this->m_hWnd) && GetFocus() == this ) { if( m_dateFormat == shortFormat ) { SetError(!IsValid()); } } return m_value; }
and m_value is defined as Date m_value; The error I am getting iserror C2220: warning treated as error - no object file generated C:\viewstr\ptp6970-DateChange\Swaps\ptp\src\eqslowerinterestleg.cpp(2039) : warning C4239: nonstandard extension used : 'argument' : conversion from 'class Date' to 'class Date &' A reference that is not to 'const' cannot be bound to a non-lvalue
I dont understand why I get this error and I dont believe the solution is to pass in by const reference since I modify the argument in the function. Can someone please explain why I get this error and a solution? I have found that if I doDate xx = m_jdtTerminationDate.GetValue(); OnLoseFocusDate(xx);
then it is ok but I dont understand the difference between the 2 methods. Many thanks ! -
Hi I am trying to pass by reference a variable. Below is the code. In the class def I have
void OnLoseFocusDate( Date &jdtDate);
and in the src file I havevoid CEQSLowerInterestLeg::OnLoseFocusDate( Date &jdtDate) { if( jdtDate != Util::grdtNull) { Date adjust_date( jdtDate ); const CCalendar *calendar = CHolidayMgr::GetInstance().GetEquityLegPaymentHoliday(); if (calendar) { adjust_date = calendar->BusinessDayIf(adjust_date, (EnBusDayConvention)m_jcmBDC.GetSelectedObjectID(), &ToolUtils::PromptForBusinessDay); jdtDate = adjust_date; } } } // end of OnLoseFocusDate
with the function call beingOnLoseFocusDate(m_jdtTerminationDate.GetValue());
where GetValue() is defined asDate NewDate::GetValue(void) { if (::IsWindow(this->m_hWnd) && GetFocus() == this ) { if( m_dateFormat == shortFormat ) { SetError(!IsValid()); } } return m_value; }
and m_value is defined as Date m_value; The error I am getting iserror C2220: warning treated as error - no object file generated C:\viewstr\ptp6970-DateChange\Swaps\ptp\src\eqslowerinterestleg.cpp(2039) : warning C4239: nonstandard extension used : 'argument' : conversion from 'class Date' to 'class Date &' A reference that is not to 'const' cannot be bound to a non-lvalue
I dont understand why I get this error and I dont believe the solution is to pass in by const reference since I modify the argument in the function. Can someone please explain why I get this error and a solution? I have found that if I doDate xx = m_jdtTerminationDate.GetValue(); OnLoseFocusDate(xx);
then it is ok but I dont understand the difference between the 2 methods. Many thanks !The problem is that when you return something from a function (for example your GetValue function), a copy of your object will be made (you don't return the object itself but a copy). So in your case, passing it by reference would mean that the function (OnLoseFocusDate) will probably want to modify it. But in your case it is totally useless because the object is lost after the function is called (so, the modifications are lost). One way to do it is to return a reference to your object in the GetValue function:
Date**&** NewDate::GetValue(void) { if (::IsWindow(this->m_hWnd) && GetFocus() == this ) { if( m_dateFormat == shortFormat ) { SetError(!IsValid()); } } return m_value; }
But honnestly, I don't really find this very elegant...
Cédric Moonen Software developer
Charting control [v1.1] -
Hi I am trying to pass by reference a variable. Below is the code. In the class def I have
void OnLoseFocusDate( Date &jdtDate);
and in the src file I havevoid CEQSLowerInterestLeg::OnLoseFocusDate( Date &jdtDate) { if( jdtDate != Util::grdtNull) { Date adjust_date( jdtDate ); const CCalendar *calendar = CHolidayMgr::GetInstance().GetEquityLegPaymentHoliday(); if (calendar) { adjust_date = calendar->BusinessDayIf(adjust_date, (EnBusDayConvention)m_jcmBDC.GetSelectedObjectID(), &ToolUtils::PromptForBusinessDay); jdtDate = adjust_date; } } } // end of OnLoseFocusDate
with the function call beingOnLoseFocusDate(m_jdtTerminationDate.GetValue());
where GetValue() is defined asDate NewDate::GetValue(void) { if (::IsWindow(this->m_hWnd) && GetFocus() == this ) { if( m_dateFormat == shortFormat ) { SetError(!IsValid()); } } return m_value; }
and m_value is defined as Date m_value; The error I am getting iserror C2220: warning treated as error - no object file generated C:\viewstr\ptp6970-DateChange\Swaps\ptp\src\eqslowerinterestleg.cpp(2039) : warning C4239: nonstandard extension used : 'argument' : conversion from 'class Date' to 'class Date &' A reference that is not to 'const' cannot be bound to a non-lvalue
I dont understand why I get this error and I dont believe the solution is to pass in by const reference since I modify the argument in the function. Can someone please explain why I get this error and a solution? I have found that if I doDate xx = m_jdtTerminationDate.GetValue(); OnLoseFocusDate(xx);
then it is ok but I dont understand the difference between the 2 methods. Many thanks !minkowski wrote:
Date xx = m_jdtTerminationDate.GetValue(); OnLoseFocusDate(xx);
A Reference is in effect, a Compiler Generated and Guaranteed Pointer. Unlike Ordinary Poiners, they cannot be NULL, and in most cases, when you try to do something daft, the compiler will object. This happened in this case! A Reference implicitly points to a value in memory. The result of a Function is essentially not a value in memory, so the compiler objects, It is WRONG! Your Second attempt works, because the reference is an implicit pointer to xx
LateNightsInNewry