Click here to Skip to main content
16,007,111 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: allocate memory Pin
Bernhard12-Sep-01 0:27
Bernhard12-Sep-01 0:27 
GeneralRe: allocate memory Pin
Gérald Mercet12-Sep-01 0:36
Gérald Mercet12-Sep-01 0:36 
GeneralRe: allocate memory Pin
Tomasz Sowinski12-Sep-01 0:57
Tomasz Sowinski12-Sep-01 0:57 
GeneralRe: allocate memory Pin
Steen Krogsgaard12-Sep-01 2:36
Steen Krogsgaard12-Sep-01 2:36 
GeneralRe: allocate memory Pin
Gérald Mercet12-Sep-01 3:15
Gérald Mercet12-Sep-01 3:15 
GeneralRe: allocate memory Pin
Steen Krogsgaard12-Sep-01 3:59
Steen Krogsgaard12-Sep-01 3:59 
GeneralRe: allocate memory Pin
Gérald Mercet12-Sep-01 5:03
Gérald Mercet12-Sep-01 5:03 
GeneralRe: allocate memory Pin
Steen Krogsgaard12-Sep-01 21:37
Steen Krogsgaard12-Sep-01 21:37 
Hi Gerald,

Forget about GetDates() in my last post. It was overcomplicated and actually dead-wrong Blush | :O . The idea was to encapsulate the m_pDates pointer. In this way you can ensure that only your CMyClass class have permission to allocate and delete the COleDateTime objects that m_pDates points to. By hiding the member variable (m_pDates) and only allow access to it though member functions (AllocTwoDates, DeleteDates and GetDates) you can control the allocation and deletion - only AllocTwoDates gets to allocate, only DeleteDates gets to delete, and everybody else can read, but not write, the pointer to the dates through GetDates(). But the solution I gave you didn't work. Here's why. The operator delete will not accept a pointer-to-a-const (e.g. const COleDateTime*) as an argument, so I thought that if I let GetDates() return a const COleDateTime* I could avoid the users of your CMyClass to write something like
GetDates()[1].Format("%d %b %Y");
delete GetDates();

as it should only be allowed to delete the date objects through the member function DeleteDates(). And this does actually work. However, by making the returned pointer a pointer-to-const I also remove any possibility to modify the date objects:
GetDates()[0] = COleDateTime::GetCurrentDate(); // illegal

and that is _not_ what you wanted.
So, just forget this solution.

Your question actually boils down to one of scope. If you want to preserve the date objects between calls to CMyClass member functions you should save the pointer returned from operator new in a member variable. And you should make sure that you delete the date objects when you no longer needs them (that's what the call to DeleteObjects() in the destructor makes sure of).

Anyway, do you always need exactly two date objects (and not one or three)? If so I would recommend that you make the objects members of your class instead of allocating them on the heap:
class CMyClass
{
public:
   COleDateTime Dates[2];
   ...
};

this way you avoid all the allocation/deletion stuff.

Cheers
Steen.

"To claim that computer games influence children is rediculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"
GeneralRe: allocate memory Pin
Gérald Mercet12-Sep-01 23:32
Gérald Mercet12-Sep-01 23:32 
GeneralRe: allocate memory Pin
Steen Krogsgaard12-Sep-01 23:57
Steen Krogsgaard12-Sep-01 23:57 
GeneralRe: allocate memory Pin
Gérald Mercet13-Sep-01 5:56
Gérald Mercet13-Sep-01 5:56 
GeneralRe: allocate memory Pin
Steen Krogsgaard13-Sep-01 21:20
Steen Krogsgaard13-Sep-01 21:20 
GeneralRe: allocate memory Pin
Gérald Mercet13-Sep-01 22:51
Gérald Mercet13-Sep-01 22:51 
GeneralRe: allocate memory Pin
Steen Krogsgaard13-Sep-01 23:00
Steen Krogsgaard13-Sep-01 23:00 
GeneralRe: allocate memory Pin
Gérald Mercet13-Sep-01 23:39
Gérald Mercet13-Sep-01 23:39 
GeneralRe: allocate memory Pin
Steen Krogsgaard13-Sep-01 23:48
Steen Krogsgaard13-Sep-01 23:48 
GeneralRe: allocate memory Pin
Gérald Mercet14-Sep-01 2:37
Gérald Mercet14-Sep-01 2:37 
GeneralRe: allocate memory Pin
Steen Krogsgaard14-Sep-01 2:53
Steen Krogsgaard14-Sep-01 2:53 
GeneralRe: allocate memory Pin
Gérald Mercet14-Sep-01 3:24
Gérald Mercet14-Sep-01 3:24 
GeneralRe: allocate memory Pin
Steen Krogsgaard14-Sep-01 3:27
Steen Krogsgaard14-Sep-01 3:27 
GeneralRe: allocate memory Pin
Gérald Mercet12-Sep-01 4:00
Gérald Mercet12-Sep-01 4:00 
GeneralRe: allocate memory Pin
Steen Krogsgaard12-Sep-01 4:07
Steen Krogsgaard12-Sep-01 4:07 
Questionwhile statement probleam, please help? Pin
Tea CH11-Sep-01 17:31
Tea CH11-Sep-01 17:31 
AnswerRe: while statement probleam, please help? Pin
Christian Graus11-Sep-01 17:47
protectorChristian Graus11-Sep-01 17:47 
GeneralRe: while statement probleam, please help? Pin
Tea CH11-Sep-01 20:19
Tea CH11-Sep-01 20:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.