473,409 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

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 1757
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kevin Saff | last post by:
Apparently I'm missing something. Stroustrup (15.3) says of protected access: If is protected, its name can be used only by member functions and friends of the class in which it is declared...
2
by: diadia | last post by:
when i write two classes as follow . compiler tell me that i can't access private number from other class but if types of two classes are the same it don't have error message ? why? class...
12
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
22
by: Nivvy | last post by:
HI all, I just wondered by seeing the o/p of the program. Let me know wt is the reason. class Base { public : virtual void f1() {
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
5
by: Colin Anderson | last post by:
I discovered, with great excitement, this article http://www.davison.uk.net/vb2notes.asp when researching methods for emailing from Access via Notes. Unfortunatly, when I run this I get a...
4
by: lars.uffmann | last post by:
Hey everyone! I am (still) working on a project that I took over from former students, so don't blame me for the criminal approach on coding *g* The problem I have is fairly easy and while I...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
3
by: dolphin | last post by:
Hello everyone! Can a static member function access non-static member? I think it is illegal.Is it right?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.