|
Hi experts...
What about this?
_tcscpy(sEndDate,CurrentDate);
I am using this style. Is this wrong or right way?
modified on Tuesday, May 27, 2008 7:42 AM
|
|
|
|
|
That makes a copy. It is fine sometimes (and the other times it is wrong).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Sir, just tell me where it will fail? I want to clear my confusion.
Thanks
|
|
|
|
|
Whenever you need to actually modify CString 's internal buffer.
It is not a common usage, I know, but it is perfectly legal.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Thank you sir
|
|
|
|
|
In general an explicit function call should be preferred over an implicit cast so it's not wrong but a GetBuffer() call would be better style and of course you should otherwise be using _tcscpy_s
"The secret of happiness is freedom, and the secret of freedom, courage."
Thucydides (B.C. 460-400)
|
|
|
|
|
Matthew Faithfull wrote: it's not wrong but a GetBuffer() call would be better style
I don't agree. Implicit (or explicit) cast is not the same as GetBuffer() and you shouldn't use optionally one or the other: GetBuffer returns LPTSTR , while the cast returns LPCTSTR : the added C have his significance.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
CPallini wrote: the added C have his significance.
Indeed it has and although I was talking general C++ style I do think it applies in this case. The CString impilcit cast returns LPCSTR because it isn't safe for it to hand out a pointer to its internal buffer without locking it but it also isn't good C++ for it to 'silently' give you a const pointer to something that is inherently not const. It's a compromise brought on by a compromised design.
"The secret of happiness is freedom, and the secret of freedom, courage."
Thucydides (B.C. 460-400)
|
|
|
|
|
Well, let's try to get it from the CString 's consumer point of view:
(1) requesting, via (explicit) cast a pointer to a const buffer means: "OK, I need the buffer but I'll not change it".
(2) requesting via GetBuffer() a pointer to the internal buffer means: "I need the buffer to make all the weirdest things I know to it".
Clearly method (2) is a bit crude for a mere copy operation.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
hi der, da getbuffr iz renamed in da latast sdk as GetBufferIKnowWhatImDoing()
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Indeed!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
I strongly second that.
GetBuffer() is really to be forbidden for cast purpose
|
|
|
|
|
Maxim Zarus wrote: Is this wrong or right way?
It's wrong since sEndDate has no storage space; it's just a pointer (to wherever).
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
you mean, first allocate memory for sEndDate then i can use _tcscpy_s() .
its ok. i agree.
but is it also neccessary to allocate memory when CString::GetBuffer() has been used?
because i have seen in pervious post nobody ask to allocate memory for CString::GetBuffer() .
regards,
Maxim...
|
|
|
|
|
Maxim Zarus wrote: you mean, first allocate memory for sEndDate then i can use _tcscpy_s().
Yes.
Maxim Zarus wrote: but is it also neccessary to allocate memory when CString::GetBuffer() has been used?
Not necessarily. It all depends on what you are going to be doing with the returned pointer.
Maxim Zarus wrote: because i have seen in pervious post nobody ask to allocate memory for CString::GetBuffer().
CString::GetBuffer() is very often misused.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
You can even use macrs A2W and W2A for conversions from multibyte-widechar and widechar-multibyte
For these macros you need to include "atlconv.h"
and also you need to write USES_CONVERSION macro before using these macros inside function.
|
|
|
|
|
|
please anybody can help me with the name of a code profiler tool for unmanaged C++ ?
I do'nt like something so much sophisticated as VTune..
I rather want a simple tool through which I can mark code regions and then after exection it just report to me the time spent on each region and the relative duration ratio for each region..
In another word is there something like SlickEdit Tools quick profiling feature, but rather for C++ unamanged code ?
|
|
|
|
|
See for IBM Rationals Code Coverage tool that comes with Purify plus
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
Hi,
How can I create Progress bar in the Listview subitem?
|
|
|
|
|
there are many samples on code project.
|
|
|
|
|
Click here[^]
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
|
I want to know how through use of MFC API we can do nslookup and query our DNS server to look for smtp mail server, responsible for forwarding our messages to other smtp servers on same network(doamin) or some other domain.
|
|
|
|
|
You can use DnsQuery() for this.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|