Click here to Skip to main content
15,905,877 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Casts Pin
CPallini5-Jun-07 2:42
mveCPallini5-Jun-07 2:42 
AnswerRe: Casts Pin
Paresh Chitte5-Jun-07 2:58
Paresh Chitte5-Jun-07 2:58 
QuestionRe: Casts Pin
David Crow5-Jun-07 3:12
David Crow5-Jun-07 3:12 
AnswerRe: Casts Pin
Nelek5-Jun-07 2:59
protectorNelek5-Jun-07 2:59 
GeneralRe: Casts Pin
CPallini5-Jun-07 3:14
mveCPallini5-Jun-07 3:14 
GeneralRe: Casts Pin
Nelek6-Jun-07 6:57
protectorNelek6-Jun-07 6:57 
GeneralRe: Casts Pin
jhwurmbach6-Jun-07 7:08
jhwurmbach6-Jun-07 7:08 
GeneralRe: Casts Pin
CPallini6-Jun-07 9:59
mveCPallini6-Jun-07 9:59 
Nelek wrote:
Anyway... If the char m_cName is set to 100, would it then works with longer names? or still have problems with the length?


No. AFAIK CString can hold several thousand (virtually, an infinite number of) characters, hence your code will not handle properly big CStrings.

On the overall, your code:

Nelek wrote:

//In .hchar
m_cName [20];
//In .cpp
LPTSTR CMyClass::TransformTextFormat (const CString szString)
{
  for (int i = 0; i < 20; i++)
    m_cName[i] = NULL;
  for (int j = 0; j < szString.GetLength (); j++)
    m_cName[j] = szString[j];
  return &m_cName[0];
 }



has, IMHO several flaws:

(1) you should not define variables in header files (only declarations there). If you include the same header file in some source files (that is the usual scenario) then you experience compiler errors.

(2) The function, as it stands, relies on the factthat its argument has a lenght less than a certain amount (unless you use Design by contract technique...). This is dangerous: you have to handle bigger input buffers, without going out of the array boundaries (e.g. copying maximum 19 characters - you need the trailing NULL).

(3) code is inefficient, you first initialize to NULL a characther at time, then copy, a character at time, the CString into your buffer. The first operation is not required, the second one may be performed copying the whole buffer at once (of course you need to NULL terminate the string).

BTW efficience may be not a ISSUE, but reliability must be.

Hope that helps.

Smile | :)



(3)


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.

GeneralRe: Casts Pin
Nelek8-Jun-07 6:20
protectorNelek8-Jun-07 6:20 
GeneralRe: Casts Pin
jhwurmbach5-Jun-07 23:27
jhwurmbach5-Jun-07 23:27 
GeneralRe: Casts Pin
Nelek6-Jun-07 6:59
protectorNelek6-Jun-07 6:59 
GeneralRe: Casts Pin
jhwurmbach6-Jun-07 7:02
jhwurmbach6-Jun-07 7:02 
AnswerRe: Casts Pin
Paresh Chitte5-Jun-07 3:01
Paresh Chitte5-Jun-07 3:01 
JokeRe: Casts Pin
CPallini5-Jun-07 3:15
mveCPallini5-Jun-07 3:15 
GeneralRe: Casts Pin
#realJSOP5-Jun-07 3:59
professional#realJSOP5-Jun-07 3:59 
GeneralRe: Casts Pin
Stephen Hewitt5-Jun-07 14:38
Stephen Hewitt5-Jun-07 14:38 
GeneralRe: Casts Pin
jhwurmbach5-Jun-07 23:31
jhwurmbach5-Jun-07 23:31 
AnswerRe: Casts Pin
Rajesh R Subramanian5-Jun-07 4:21
professionalRajesh R Subramanian5-Jun-07 4:21 
GeneralRe: Casts Pin
Nelek6-Jun-07 7:01
protectorNelek6-Jun-07 7:01 
Questiondisabling and graying out a menu item Pin
SWDevil5-Jun-07 2:13
SWDevil5-Jun-07 2:13 
QuestionCString Splitter? Pin
AlexDrummer5-Jun-07 2:02
AlexDrummer5-Jun-07 2:02 
AnswerRe: CString Splitter? Pin
Optimus Chaos5-Jun-07 2:19
Optimus Chaos5-Jun-07 2:19 
AnswerRe: CString Splitter? Pin
David Crow5-Jun-07 2:44
David Crow5-Jun-07 2:44 
QuestionAdd button to datagridview Pin
sulabh20205-Jun-07 1:56
sulabh20205-Jun-07 1:56 
QuestionGUID Pin
saisp5-Jun-07 0:51
saisp5-Jun-07 0:51 

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.