Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

question about private member

0 views
Skip to first unread message

zhangyef...@gmail.com

unread,
Nov 28, 2008, 9:39:08 PM11/28/08
to
class String
{
public:
String & operate=(const String *other)
{
delete m_data;
m_data=new char[strlen(other.m_data)+1];
strcpy(m_data,other.m_data);
}
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.

do anyone give me any explanation ?
thanks in advance
.

red floyd

unread,
Nov 28, 2008, 11:26:27 PM11/28/08
to
zhangyef...@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?

Bill

unread,
Nov 28, 2008, 11:36:06 PM11/28/08
to

<zhangyef...@gmail.com> wrote in message
news:00419a46-05e1-4730...@b38g2000prf.googlegroups.com...

Because it's a member of the same class. It works this way because it would
be too difficult (or impossible) at compile time to determine which type of
object a pointer in a method was pointing to (IIRC, from Bjarne Stroustrup's
fine book).

Bill


zhangyef...@gmail.com

unread,
Nov 29, 2008, 1:54:45 AM11/29/08
to
On Nov 29, 12:26 pm, red floyd <no.spam.h...@example.com> wrote:

maybe you are right.thank you.
i want to say,how difficult c++ is.


Alan Johnson

unread,
Nov 29, 2008, 2:18:05 AM11/29/08
to

While I don't necessarily disagree, this is a strange choice of language
features to use to claim C++ is difficult. That is how private class
members work in pretty much every language I've encountered that
supports the concept.

Bill

unread,
Nov 29, 2008, 2:45:13 AM11/29/08
to

"Bill" <Bill_...@comcast.net> wrote in message
news:ggqgr...@news6.newsguy.com...


By "which type", of course, I meant the invoking object or the one in the
parameter list, or some other object of the same type. I hope that helps.
It's not that it "should" be this way--it's that the alternatives, in view
of the implementation of the language translator, leave few choices.

Bill


Juha Nieminen

unread,
Nov 29, 2008, 7:13:50 PM11/29/08
to
zhangyef...@gmail.com wrote:
> according to Principle of Encapsulation ,a boject can not access other
> object's private memeber.

How would you implement a copy constructor or copy assignment operator
if an object cannot access the private members of another object of the
same type?

If you have a pointer to an object of the same type as the current
one, how would the compiler know if that pointer is pointing to another
object or to 'this'?

0 new messages