5/26/08

How many problems can you spot in the following class definition?

class MyString()
{
MyString()
{
}
~MyString()

{
delete m_pText;
}

void Set(char* pText)
{
size_t length = strlen(pText);
m_pText = new char[length];

}

private:
m_pText;

}
int _tmain(int argc, _TCHAR* argv[])

{ return 0;
}

Answers:


  1. Class declaration is wrong ended with ().
  2. Type for m_pText is not specified.
  3. Class declaration closing brace is not ended with semicolon
  4. m_pText should be deleted as delete[] and not just delete.
  5. you cant take the strlen of pointer as if it is not null terminated one it will give wrong results.

No comments: