473,382 Members | 1,390 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,382 software developers and data experts.

access to private member of same class type

psp
I'm really puzzled why this is allowed by the c++ compiler:
class C {
public:
void setNumber( int y ) { m_y = y; }
void printNumber() const { cout << "m_y= " << m_y << endl; }
void copyInt(C &anotherCObj) { m_y = anotherCObj.m_y; }
private:
int m_y;
};

Is there a c++ rule/specification that allows access to private
members of a class if accessed from the same class.

Jun 22 '07 #1
5 1754
psp wrote:
I'm really puzzled why this is allowed by the c++ compiler:
class C {
public:
void setNumber( int y ) { m_y = y; }
void printNumber() const { cout << "m_y= " << m_y << endl; }
void copyInt(C &anotherCObj) { m_y = anotherCObj.m_y; }
private:
int m_y;
};

Is there a c++ rule/specification that allows access to private
members of a class if accessed from the same class.
Yes. Access is granted. Why is it surprizing to you? That's how
it has been for decades.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '07 #2
On Jun 21, 8:44 pm, psp <pradhan.push...@gmail.comwrote:
I'm really puzzled why this is allowed by the c++ compiler:
class C {
public:
void setNumber( int y ) { m_y = y; }
void printNumber() const { cout << "m_y= " << m_y << endl; }
void copyInt(C &anotherCObj) { m_y = anotherCObj.m_y; }
private:
int m_y;

};

Is there a c++ rule/specification that allows access to private
members of a class if accessed from the same class.
Imagine how silly it would be if every time you wanted to write an
equality operator or copy constructor you had to write something like
this:

class A
{
friend class A;
};

Jun 22 '07 #3
psp wrote:
I'm really puzzled why this is allowed by the c++ compiler:
class C {
public:
void setNumber( int y ) { m_y = y; }
void printNumber() const { cout << "m_y= " << m_y << endl; }
void copyInt(C &anotherCObj) { m_y = anotherCObj.m_y; }
private:
int m_y;
};

Is there a c++ rule/specification that allows access to private
members of a class if accessed from the same class.
Yes. If this is surprsing to you then you have misunderstood the purpose
of private.

The purpose of data hiding (i.e. making things private) is to seperate
trusted code (i.e. all the code in class C, plus any friend code) from
untrusted code. This is useful in the real world.

The purpose is not to seperate objects from each other. That would make
data hiding harder, because every object would have to have public
accessors for everything, otherwise it would be impossible to copy or
compare objects.

john
Jun 22 '07 #4
On Jun 21, 8:44 pm, psp <pradhan.push...@gmail.comwrote:
I'm really puzzled why this is allowed by the c++ compiler:
class C {
public:
void setNumber( int y ) { m_y = y; }
void printNumber() const { cout << "m_y= " << m_y << endl; }
void copyInt(C &anotherCObj) { m_y = anotherCObj.m_y; }
private:
int m_y;

};

Is there a c++ rule/specification that allows access to private
members of a class if accessed from the same class.
Yes. If class members and methods were completely private to
everything, there would be no point to it. There would be no way to
access the private data or functions, making that dead code.

Jun 22 '07 #5
Mikey wrote:
On Jun 21, 8:44 pm, psp <pradhan.push...@gmail.comwrote:
>I'm really puzzled why this is allowed by the c++ compiler:
class C {
public:
void setNumber( int y ) { m_y = y; }
void printNumber() const { cout << "m_y= " << m_y << endl; }
void copyInt(C &anotherCObj) { m_y = anotherCObj.m_y; }
private:
int m_y;

};

Is there a c++ rule/specification that allows access to private
members of a class if accessed from the same class.

Yes. If class members and methods were completely private to
everything, there would be no point to it. There would be no way to
access the private data or functions, making that dead code.
That's not entirely true. The private members could have been made
to be only accessible through 'this->'. It would certainly make more
secure execution envinonment, but the use would be rather limited.
There were several discussions in the past years about a specifier
like "very_private" which would restrict access to only the current
instance. My guess is that due to its limited usefulness it wasn't
made part of the language.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Richard Webb | last post by:
Hi all, I guess this is more of a design problem than a language problem, but I'm confused either way! I have a class and it has a private data member which is a struct. The size of the struct is...
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 )...
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
6
by: Alf P. Steinbach | last post by:
#include <iostream> struct Belcher { int x; void belch() { std::cout << x << std::endl; } Belcher( int anX ): x( anX ) {} }; struct Person: private Belcher { Person(): Belcher( 666 ) {} };
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...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
3
by: Chua Wen Ching | last post by:
Hi there, I had seen examples for classes, but i had no idea how to implement the same thing in struct. I am quite mix up! Which one is correct? Scenario: WForm.cs - the one that calls...
9
by: Mike | last post by:
Hi, Just a simple question: why the compiler doesn't report error when accessing a private member function inside a function having template type ? For example: #include<iostream> using...
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:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.