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

Z

zqhpnp

class String
{
public:
String& operator=(const String& str);
private:
char* pdata;
}
String& String::eek:perator=(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.
 
K

Kristo

class String
{
public:
String& operator=(const String& str);
private:
char* pdata;
}
String& String::eek:perator=(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
 
F

Ferdi Smit

class String
{
public:
String& operator=(const String& str);
private:
char* pdata;
}
String& String::eek:perator=(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: (e-mail address removed)
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
 
V

Victor Bazarov

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::eek:perator=(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
 
K

Kaz Kylheku

class String
{
public:
String& operator=(const String& str);
private:
char* pdata;
}
String& String::eek:perator=(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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top