470,614 Members | 1,556 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

Why an obeject can access the others objects's private member?

class String
{
public:
String& operator=(const String& str);
private:
char* pdata;
}
String& String::operator=(const String& str)
{
if(this==&str)
return;
delete[] pdata;
pdata=new char[strlen(str.pdata)+1];
if(pdata==0)
return;
strcpy(pdata,str.pdata);
return *this;
}
str.pdata seem to be invalid,but the compiler(VC 6.0) dosen't report
errors.Why the object pointed by this pointer can access the
object's(referrenced by str) member pdata.Whether we can do like that
in specific functions(copy constructor...?).
Who can give me an answer? Thanks in advance.

Oct 21 '05 #1
5 1631
zq****@gmail.com wrote:
class String
{
public:
String& operator=(const String& str);
private:
char* pdata;
}
String& String::operator=(const String& str)
{
if(this==&str)
return;
delete[] pdata;
pdata=new char[strlen(str.pdata)+1];
if(pdata==0)
return;
strcpy(pdata,str.pdata);
return *this;
}
str.pdata seem to be invalid,but the compiler(VC 6.0) dosen't report
errors.Why the object pointed by this pointer can access the
object's(referrenced by str) member pdata.Whether we can do like that
in specific functions(copy constructor...?).
Who can give me an answer? Thanks in advance.


Member functions can access private data for any object of the class,
not just the invoking object. Accessing str.pdata as shown above is
perfectly legal.

Kristo

Oct 21 '05 #2
zq****@gmail.com wrote:
class String
{
public:
String& operator=(const String& str);
"The Rule of Three" is not followed here. Perhaps you didn't post the
entire code?
private:
char* pdata;
}
A semicolon is missing here. So, you didn't actually post the real code,
did you?
String& String::operator=(const String& str)
{
if(this==&str)
return;
delete[] pdata;
pdata=new char[strlen(str.pdata)+1];
if(pdata==0)
return;
This check is redundant. No modern compiler will return '0' from 'new'.
It will throw 'std::bad_alloc' exception. And you should let it to throw
instead of proceeding as if nothing happened.
strcpy(pdata,str.pdata);
return *this;
}
str.pdata seem to be invalid,
Invalid? In what way? If it *is* invalid, why does it only "seem" so?
It is either invalid or it is valid, there is nothing "seeming" about it.
but the compiler(VC 6.0) dosen't report
errors.Why the object pointed by this pointer can access the
object's(referrenced by str) member pdata.
Why not?
Whether we can do like that
in specific functions(copy constructor...?).
Who can give me an answer? Thanks in advance.


Access rights are defined per class, not per instance. What book are you
reading on C++ that does not explain access rights?

V
Oct 21 '05 #3
zq****@gmail.com wrote:
class String
{
public:
String& operator=(const String& str);
private:
char* pdata;
}
String& String::operator=(const String& str)
{
if(this==&str)
return;
delete[] pdata;
pdata=new char[strlen(str.pdata)+1];
if(pdata==0)
return;
strcpy(pdata,str.pdata);
return *this;
}
str.pdata seem to be invalid,but the compiler(VC 6.0) dosen't report
errors.Why the object pointed by this pointer can access the
object's(referrenced by str) member pdata.Whether we can do like that
in specific functions(copy constructor...?).
Who can give me an answer? Thanks in advance.


Yes, this is allowed. It's not so strange when you think of it: the
class itself has access to itself. You can't break anything here,
because the only code that can access the private members is code that
belongs to the class anyway. Any member function can access private
members of any other object that has it's _exact_ type. It doesn't work,
for example, if you try to access protected attributes of a base class
of String in this way.

--
Regards,

Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Oct 21 '05 #4
Now I get it.Thanks a lot,

Oct 21 '05 #5
zq****@gmail.com wrote:
class String
{
public:
String& operator=(const String& str);
private:
char* pdata;
}
String& String::operator=(const String& str)
{
if(this==&str)
return;
delete[] pdata;
pdata=new char[strlen(str.pdata)+1];
if(pdata==0)
return;
strcpy(pdata,str.pdata);
return *this;
}
str.pdata seem to be invalid,but the compiler(VC 6.0) dosen't report
errors.Why the object pointed by this pointer can access the
object's(referrenced by str) member pdata.
Because access is based on class, not on instance. The access here is
taking place within the scope of the String class, because the
accessing expression appears within the body of a function in that
class. It is that class scope which gives the expression the special
power to access private members in that class.

Access restriction is about program modularity. It is reasonable to
give the entire module free reign over doing whatever it has to do to
make the objects work. In C++, a class is loosely equivalent to a
module.
Whether we can do like that
in specific functions(copy constructor...?).


What if a copy constructor's author wants to delegate the job to some
other function? If only a copy constructor or assignment operator could
access private members in the right hand side object, it would be
difficult, if not impossible, to factor their logic into subordinate
functions.

Oct 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Kevin Saff | last post: by
2 posts views Thread by diadia | last post: by
6 posts views Thread by Peter Frost | last post: by
15 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.