|
Sorted, thank you very much, much appreciated.
Nick
|
|
|
|
|
This is exactly what I'm doing, but in WM_COMMAND all WPARAM wp ever is is 0x0000FFFF
any ideas why?
I set it to
#define BTN_ONE 301
Thanks
|
|
|
|
|
I would like to create a function which could return 2 COleDateTime objects simultaneously, so i think that i could use a pointer of COleDateTime. But, as i'm a beginner in C++, i would like to know if someone could show me how we could do the allocation of this pointer, his deletion, and how i could return the dates, as i want the function return dates?
thanks in advance
gerald
|
|
|
|
|
You create the Objects dynamically
COleDateTime* pDate1 = new COleDateTime();
COleDateTime* pDate2 = new COleDateTime();
These objects are not gonna be destroyed if you leave the scope of this function !!
You can acess all the member - Fun´s and Var´s by using the -> operator:
pDate1->GetStatus();
You wanna get rid of them:
delete pDate1;
delete pDate2;
if this helps you... fine.. if not.. you can ask me..
Bernhard
- I heard if you play the Windows CD backwards, you get a satanic message.
- That's nothing, if you play it forward, it installs Windows!
|
|
|
|
|
ok with your answer, but how could i return the values of 2 dates with 1 function?
|
|
|
|
|
May I suggest you use a struct holding the two dates just like this:
typedef struct{
COleDateTime firstDateTime;
COleDateTime secondDateTime;
}PairCOleDateTime; and have your function return a PairCOleDateTime instead of a pointer. This way you get rid of all sorts of allocation/deallocation problems, and the extra cost of returning this struct by value should be negligible for most purposes.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
and if i define this struct, how could i use it in a function which modify or use the dates?
|
|
|
|
|
void GetTime(COleDateTime* pDate1, COleDateTime* pDate2)
{
pDate1 = new COleDateTime();
pDate2 = new COleDateTime();
//Fill the Objects with Data or do whatever you want to do with them
}
//Your Main Function
void WaWa()
{
COleDateTime* pDate1 = 0;
ColeDateTime* pDate2 = 0;
GetTime (pDate1, pDate2);
//Do whatever you want to do with em..
pDate1->GetTime();
pDate2->GetTime();
//if you´ve finished.. get rid of em..
delete pDate1;
delete pDate2;
}
Another version would be to allocate them in the main function (WaWa).. but that´s just an answer of how you would like to do this..
i hope this helped you..
Bernhard
- I heard if you play the Windows CD backwards, you get a satanic message.
- That's nothing, if you play it forward, it installs Windows
|
|
|
|
|
my function take 2 coledatetime parameters AND modify them and returnthe modification on this 2 dates, but i don't know how to do that!
|
|
|
|
|
I think using references would be easiest way:
void myFunc(COleDateTime & d1, COleDateTime & d2)
{
}
COleDateTime x, y;
myFunc(x, y);
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
If you would like to allocate an array of COleDateTime objects dynamically here's how you do it:
COleDateTime* CMyClass::AllocTwoDates()
{
COleDateTime* pDate = new COleDateTime[2];
return pDate;
}
When you're through using the allocated objects delete them:
void CMyClass::SomeFunction()
{
COleDateTime* pDates = AllocTwoDates()
delete[] pDates;
}
Remember to use the delete[] operator instead of the delete operator, otherwise the constructors of the COleDateTime objects will not get called.
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"
|
|
|
|
|
So, if i want to modify the dates i have allocated first with AllocTwoDates(), for example in SomeFunction(), we could know the modifications after the use of this function even if we delete it?
|
|
|
|
|
This reply got longer than I intended, sorry about that. Anyway, here goes:
I'm not too sure what you mean. But if you delete the array of dates then all the information contained in the date objects are gone.
void CMyClass::SomeFunction()
{
COleDateTime* pDates = AllocTwoDates();
pDates[0] = COleDateTime::GetCurrentTime();
pDates[1].SetDate(pDates[0].GetYear()+1 ,pDates[0].GetMonth() ,pDates[0].GetDay());
delete[] pDates;
CString cs = pDates[1].Format(%d %b %Y);
If you want to keep the date objects after SomeFuntion() returns you should make pDates a member variable instead, do the allocation in the constructor (if possible) and the deletion in the destructor. If the number of objects in the date array changes in CMyClass' lifetime you should at least delete the pointer in the destructor:
class CMyClass
{
public:
CMyClass();
~CMyClass();
void SomeFunction();
void UseTheDates();
protected:
void AllocTwoDates();
void DeleteDates();
const COleDateTime* GetDates();
private:
COleDateTime* m_pDates;
};
CMyClass::CMyClass()
{
m_pDates = NULL;
}
CMyClass::~CMyClass()
{
DeleteDates();
}
void CMyClass::SomeFunction()
{
AllocTwoDates();
}
void CMyClass::UseTheDates()
{
if (GetDates() != NULL) {
}
}
void CMyClass::AllocTwoDates()
{
DeleteDates();
m_pDates = new COleDateTime[2];
}
void CMyClass::DeleteDates()
{
if (m_pDates != NULL) {
delete[] m_pDates;
m_pDates = NULL;
}
}
Note that I've changed AllocTwoDates so it works directly on the member variable instead of returning the pointer. There is no need to return a pointer to a private member - it's accessible via the member variable from any member function (including members of derived classes) and shouldn't be accessibel from outside CMyClass. This way you're sure that only methods in CMyClass can modify m_pDates. No derived classes or outside classes can reach m_pDates directly, but has to get the pointer through GetDates(). This method returns a pointer to const COleDateTime objects and this pointer cannot be an argument for delete[]. In effect, only DeleteDates() can do the deletion, and only AllocTwoDates() can do the allocation. Voila, no memory leaks!
Anyway, you should store the size of your m_pDates array somewhere (e.g. in a member variable, m_iSizeDates). This way you can avoid accessing illegal memory (e.g. m_pDates[2] in the example above).
Finally, the STL vector class or the MFC CArray template class may prove more suitable for you - they take care of allocation/reallocation and deletion, and you can pass them instead of a COleDateTime* to functions.
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"
|
|
|
|
|
perhaps, the 2 last questions.
1. i don't really understand what represent the function GetDates(), and if it's necessary to use it.
2. You use firstly a function SomeDates, where you do the allocation, and in a second time, you create UseTheDates(), but i could use AllocTwoDates in this last function?
thanks for your great help
gerald
|
|
|
|
|
Hi Gerald,
Forget about GetDates() in my last post. It was overcomplicated and actually dead-wrong . 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();
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"
|
|
|
|
|
so i really enjoyed by your help, and it's right, i need all the time of 2 dates and only 2 dates. So, i think that i will follow the last thing you show me.
I have another question, i know that in my application i'll need of a pointer with a variable size. In fact this size will be determined by calculation. I was proposed to declared a pointer as private or protected in the .h file. My question si:if i want to do the same things you showed me before for the dates and for this new pointer could i do the initialisation and the deletion of both in the constructor and destructor.
thanks for you help
|
|
|
|
|
If you know the calculated size of the data at construction time it's the best solution to allocate the data in the constructor. You should always delete the data in the destructor, of course only if the pointer is non-NULL. If you call operator delete on this pointer elsewhere in the program you have to make sure that you set it to NULL after deletion - this is not done automatically.
You could also consider using the auto_ptr type defined in the C++ RTL. There is, however, some issues about transfer of ownership when passing such a pointer as an argument to a function that you have to be aware of. I'm no expert, so you'll have to consult the docs on this one.
Glad to be of assistance.
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"
|
|
|
|
|
I would like to ask you another question.
in my application, i have decided to declare as public a variable COleDateTime ChangeDates[2]. then, with a function i modify 2 dates
ChangedDates[0]=date1;
ChangedDates[1]=date2;
and at the end of it i return ChangedDates.
Now, i would like to use this function to calculate a number of periods between these dates. In fact, i have for example a period, dt, and i want to know how many periods there are between date1 and date2. So, i need of the function above.
So, how i could call simply ChangedDates? May i have to call in first the 1st function, so that i need of a new variable in the 2nd function; or i just have to call ChangedDates?
Thanks in advance for your help and your patience
gerald
|
|
|
|
|
I'm not sure that I understand your question. You have a class, CYourClass, and in that class you have a memeber variable, ChangedDates, that is an array of two dates, right? Like this:
class CYourClass
{
public:
COleDateTime ChangedDates[2];
....
};
And you have a member function that assigns some values to this array:
void CYourClass::AssignValues()
{
ChangedDates[0] = date1;
ChangedDates[1] = date2;
}
Now you want to calculate how many periods of length dt there is between ChangedDates[0] and ChangedDates[1], right? And you're asking if you can access ChangedDates directly or if you need a new array of COleDateTimes? If this is the case, then the answer is that you can access ChangedDates directly, either from a member function or from outside CYourClass (since it's a public member):
int CYourClass::CalcDifference(period dt)
{
int iDifference = 0;
COleDateTime date = ChangedDates[0];
for (int i=0; ChangedDates[1]>date; i++) {
iDifference++;
date += dt;
}
return iDifference;
}
int CSomeOtherClass::CalcDifference(CYourClass yourclass, period dt)
{
int iDifference = 0;
COleDateTime date = yourclass.ChangedDates[0];
for (int i=0; yourclass.ChangedDates[1]>date; i++) {
iDifference++;
date += dt;
}
return iDifference;
}
If you use ChangedDates from inside a member function you can use it directly. If it is used from outside CYourClass you have to qualify it with the name of the CYourClass instance.
hope this was what you asked for
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"
|
|
|
|
|
In fact you understand my question, but perhaps i have some problem to understand how it works. But, in my case i need to use the ChangedDates in the same class it is defined, but if i use it to calculate the number of period; if i just call it ChangedDates[0]..., do you think i could get the right value i need?
thanks
gerald
|
|
|
|
|
Hmmm... I don't get it. Could you explain in more detail what you want to do?
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"
|
|
|
|
|
Ok, after i have posted it, i saw that i din't explain very well what i want to do. (perhaps because my english is far to be fluent!)
so i will try to give you an example.
As we saw above, in the .h i declare:
CMyClass
{
protected:
COleDateTime ChangedDates[2];
.
.
.
}
In .cpp:
Firstly i create a function which modify the dates, and affetc to ChangedDates, some new dates as you showed me before.
Now, i want to calculate number of period.
int CMyClass::CalcNumberOfPeriod(COleDateTime StartDate,COleDateTime EndDate)
{
int iNumberOfPeriod;
//the 2 dates i have in this function have to be modified, so i call the first function(which could be called ModifyDates). It's this thing which borrow me, because i don't know if i have to do a declartion like that:
COleDateTime ChangedDatesBis[2];
ChangedDatesBis=ModifyDates(StartDate,EndDate);
//And after i could use ChangedDatesBis, by calcuylating, for example, the difference between ChangedDatesBis[1] and ChangedDatesBis[0].
.
.
.
return iNumberOfPeriod;
}
i hope that this post would be more explicit
gerald
|
|
|
|
|
Since I'm from Denmark, English is not my native language either. Unfortunately my French is even worse than my English
Anyway, I'm still a little bit puzzled. What does your ModifyDates function do? Does it just assign the date argument to the protected member array like this:
COleDateTime[] CMyClass::ModifyDates(COleDateTime date1, COleDateTime date2)
{
ChangedDates[0] = date1;
ChangedDates[1] = date2;
return ChangedDates;
}
If so, then your CalcNumberOfPeriod function could look like this
int CMyClass::CalcNumberOfPeriod(COleDateTime StartDate,COleDateTime EndDate)
{
int iNumberOfPeriod;
ModifyDates(StartDate,EndDate);
.
.
.
return iNumberOfPeriod;
}
Is this what you wanted?
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"
|
|
|
|
|
the function ModifyDates doesn't assign only 2 dates, it takes Startdate and EndDate, and modify them if, for example, the day of EndDate is not the same that StartDate, so i change the day and put the new date in ChangedDate[1].
So, even if it's not the same thing you show me, the kind of calculation in ModifyDates are the same. After, i have created this function, i need to know how many period could i find between 2 dates, and this dates need to be modified before we calculate the number of period, so i need to call the function ModifyDates inside CalcNumberOfPeriod.
So, are you sure i just have to call the function ModifyDates, without declare a new variable?
|
|
|
|
|
Ahh, let me see if I get this correct.
You have two dates, StartDate and EndDate. You want to calculate how many periods there is between the two dates, but first you have to modify the two dates in some way. When you have calculated the number of periods you don't need the modified dates anymore. Right?
If so, I would forget all about member variables. Do it this way:
void CMyClass::ModifyDates(COleDateTime* pDates)
{
}
int CMyClass::CalcNumberOfPeriod(COleDateTime StartDate COleDateTime EndDate, periodtype dt)
{
COleDateTime Dates[2];
Dates[0] = StartDate;
Dates[1] = EndDate;
ModifyDates(Dates);
int iPeriods;
iPeriods = .....
return iPeriods;
}
Of course, the modified dates are lost when CalcNumberOfPeriod returns as the Dates array is automatic.
Now I can also see why you wanted to allocate the dates in your first posts. One way to do the above using heap allocated objects could be:
COleDateTime* CMyClass::AllocAndModifyDates(COleDateTime date1, COleDateTime date2)
{
COleDateTime* pDates = new COleDateTime[2];
pDates[0] = date1;
pDatse[1] = date2;
return pDates;
}
int CMyClass::CalcNumberOfPeriod(COleDateTime StartDate, COleDateTime EndDate, periodtype dt)
{
COleDateTime* pDates = AllocAndModifyDates(StartDate, EndDate);
int iPeriods;
delete[] pDates;
return iPeriods;
}
Hope I got it right this time.
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"
|
|
|
|
|