Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a simple question about initialisation of pointers

Status
Not open for further replies.

jayjay60

Programmer
Jun 19, 2001
97
FR
I have an application, with several functions, and one of these have to return a pointer on COleDateTime. I know the size of the "array" of COleDateTime dates, 2, but i meet a problem when i start debugging when i look in the debug window.
When i declare in the function a COleDateTime pointer i could use 2 way to do that.
Firstly, i just could write:
COleDateTime pDates[2]; and in the debug window i could see:
pDates
[0]->m_dt
->m_status
[1]->m_dt
->m_status

and in another way i could do it:
COleDateTime *pDates=new COleDateTime[2];

and in debug window i only see that:
pDates
->m_dt
->m_status

So, what is the good way if i want to return 2 dates different?

Thanks in advance

jayjay
 
If you are creating an object inside a function and you want to return a pointer &/or reference to that object then it must be created on the heap (e.g. using new keyword). If you use your first tchnique
COleDateTime pDates[2];
In this case the pointer will not be pointing to a valid address after you exit the function.
 
It's better that you use 'vector':

/// somewhere in header file
#include "vector"
using namespace std;

.
.
.

/// in your function
vector<COleDateTime> function()
{
vector<COleDateTime> Dates(2);
.
.
.
Dates[0] = ....;
.
.
.
return Dates;
}
 
Unless I'm missing something, your second method should work. What you see in the debug window is somewhat misleading. In order to see the second COleDateTime, you would need to view (pDates + 1) - which is a pointer to the second object. pDates, as a COleDateTime pointer, only refers to the first object.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top