Object pointer as method argument
-
I have a CData derived from CObject. In my CDocument, I have a CMap, with key as int, and values as CData*. In a CMyDocument method I have:
void CMyDocument::GetData(int nItemData, CData* pData/* = NULL*/)
{
m_MapData.Lookup(nItemData, pData);
TRACE("Inside doc method: pData is not NULL: %d\n", NULL != pData);
}and in my CMyView:
int nItemData = 1; CData\* pData = NULL; GetDocument()->GetData(nItemData, pData)); TRACE("In MyView: pData is not NULL: %d\\n", NULL != pData);
and here is the result: Inside doc method: pData is not NULL: 1 In MyView: pData is not NULL: 0 Why inside CMyDocument I have a valid pData, but outside not ?
-
I have a CData derived from CObject. In my CDocument, I have a CMap, with key as int, and values as CData*. In a CMyDocument method I have:
void CMyDocument::GetData(int nItemData, CData* pData/* = NULL*/)
{
m_MapData.Lookup(nItemData, pData);
TRACE("Inside doc method: pData is not NULL: %d\n", NULL != pData);
}and in my CMyView:
int nItemData = 1; CData\* pData = NULL; GetDocument()->GetData(nItemData, pData)); TRACE("In MyView: pData is not NULL: %d\\n", NULL != pData);
and here is the result: Inside doc method: pData is not NULL: 1 In MyView: pData is not NULL: 0 Why inside CMyDocument I have a valid pData, but outside not ?
-
In case 1 you have not shown the value of pData. In case 2 you have explicitly set its value to NULL.
-
I have a CData derived from CObject. In my CDocument, I have a CMap, with key as int, and values as CData*. In a CMyDocument method I have:
void CMyDocument::GetData(int nItemData, CData* pData/* = NULL*/)
{
m_MapData.Lookup(nItemData, pData);
TRACE("Inside doc method: pData is not NULL: %d\n", NULL != pData);
}and in my CMyView:
int nItemData = 1; CData\* pData = NULL; GetDocument()->GetData(nItemData, pData)); TRACE("In MyView: pData is not NULL: %d\\n", NULL != pData);
and here is the result: Inside doc method: pData is not NULL: 1 In MyView: pData is not NULL: 0 Why inside CMyDocument I have a valid pData, but outside not ?
-
In case 1, the trace show me that pData is not NULL, and after GetData call, I set up nothing, I only display if pData is NULL or not ... but is NULL ...
-
Because the
CData
pointer is passed by value. If you need to modify the pointer then you have to pass it by reference (or use a double pointer).