zhangyefei.ye...@gmail.com wrote:
I'm assuming you're doing this as a learning exercise, otherwise you
should be using std:;string.> class String
{
public:
String & operate=(const String *other)
This is incorrect.
String& operator=(const String& other)> {
delete m_data;
Undefined behavior.
m_data=new char[strlen(other.m_data)+1];
strcpy(m_data,other.m_data);
This is bad with regard to exception safety.
char *tmp = new [strlen(other.m_data) + 1];
strcpy(tmp, other.m_data);
delete[] m_data; // note delete[], not delete
m_data = tmp;
}
private:
char *m_data;
}
in the above calss, why memeber function can access the object other's
private m_data?
according to Principle of Encapsulation ,a boject can not access other
object's private memeber.
Because privacy is at the *CLASS* level, not the object level.
Think about it. How would you write a copy constructor or assignment
operator if you couldn't access the private parts of the "other" object?
maybe you are right.thank you.