Reputation: 105
i am using this codes.
Maybe it easy but now i can not. Please help me about this. I am looking always NULL in this function.
How can i solve this problem? I can not do it.
Thanks a lot.
Codes:
int my_len(const char* p) {
int c = 0;
while (*p != '\0')
{
c++;
*p++;
}
return c;
}
String::String()
:m_str(NULL)
{
}
String::String(char * other_str)
{
}
{
int mystrlen = my_len(m_str);
int myrhslen = my_len(other_str.m_str);
if (mystrlen != myrhslen)
{
return false;
}
else
{
for (int i = 0; i < mystrlen; i++)
{
if (m_str[i] != other_str.m_str[i])
{
return false;
}
}
return true;
}
}
}
Upvotes: 0
Views: 290
Reputation: 1341
Your non-default constructor has an empty implementation:
String::String(char * other_str)
{
}
So here m_str
is left uninitialized.
You could maybe copy the string, if that is your intention like so:
String::String(char * other_str)
{
m_str = strdup(other_str);
}
But then you will have to manage the memory allocated by strdup yourself, e.g. in the destructor:
String::~String()
{
if (m_str != NULL)
free(m_str);
}
Upvotes: 1