473,795 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Private member accessible from another object?

Hi all,

I read somewhere that private members of some instance of class A are
accessible from another instance of the same class. In other words,
access to private members is done 'on a class by class basis, not on an
instance by instance basis'. I couldn't quite believe it, and tried it
out for myself, and was shocked to see it was true... What is the
reasoning behind this? Why should one Person object be able to see
another Person object's private parts?

Cheers,
Siam

Aug 1 '06 #1
6 1705
Siam wrote:
I read somewhere that private members of some instance of class A are
accessible from another instance of the same class.
That's correct.
In other words,
access to private members is done 'on a class by class basis, not on
an instance by instance basis'.
Right.
I couldn't quite believe it,
Why?
and
tried it out for myself, and was shocked to see it was true... What
is the reasoning behind this?
It's done for simplicity, performance. Get a copy of D&E and read it.
Why should one Person object be able to
see another Person object's private parts?
I think you're assigning too much weight to real-world analogy here.
Access specifiers in C++ exist for different reasons than privacy laws
and moral foundations in the real world.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #2
I couldn't quite believe it,
>
Why?
I dont see why encapsulation of private data shouldnt hold between
objects of the same class. They are objects in their own right, and
their members have been made private for a good reason.
I think you're assigning too much weight to real-world analogy here.
Access specifiers in C++ exist for different reasons than privacy laws
and moral foundations in the real world.
Hehe maybe, but I'm sure I can think up plenty of real examples where
it's downright dangerous to allow other objects (albeit of the same
type) accessing your private members.

Does this rule also apply extend to inherited classes? Can an object of
a derived class access private members of an object of the base class,
and vice versa? (My guess would be yes and no, respectively... )

Siam

Aug 1 '06 #3
Siam wrote:
>>I couldn't quite believe it,

Why?

I dont see why encapsulation of private data shouldnt hold between
objects of the same class. They are objects in their own right, and
their members have been made private for a good reason.
Well, vast majority of C++ programmers don't think so or don't care.
Access specifiers don't exist for security, they exist to help create
software that works well.
>I think you're assigning too much weight to real-world analogy here.
Access specifiers in C++ exist for different reasons than privacy
laws and moral foundations in the real world.

Hehe maybe, but I'm sure I can think up plenty of real examples where
it's downright dangerous to allow other objects (albeit of the same
type) accessing your private members.
OK, you're on!
Does this rule also apply extend to inherited classes? Can an object
of a derived class access private members of an object of the base
class, and vice versa? (My guess would be yes and no, respectively... )
Please refer to your C++ textbook for the answer.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #4
Siam schrieb:
>>I couldn't quite believe it,
Why?

I dont see why encapsulation of private data shouldnt hold between
objects of the same class. They are objects in their own right, and
their members have been made private for a good reason.
Think about your class person and it's copy constructor.
How would it copy the private parts of the class? Should you declare
the class person as its own friend?

--
Thomas
Aug 1 '06 #5
Thomas J. Gritzan wrote:
Siam schrieb:
>>>I couldn't quite believe it,
Why?

I dont see why encapsulation of private data shouldnt hold between
objects of the same class. They are objects in their own right, and
their members have been made private for a good reason.

Think about your class person and it's copy constructor.
How would it copy the private parts of the class? Should you declare
the class person as its own friend?
Well, no, but you're trying to explain the [access] mechanism in its own
terms ["declare as friend"]. What the OP would rather see is something
of this sort:

class Person {
private:
Type stuff;
really_private:
OthertypeType valueable_stuff ;
public:
Person(Person& from) : stuff(from.stuf f),
// now, pay attention here:
valueable_stuff (
from.grant_spec ial_access(this , &Person::valuea ble_stuff)
)
{}

template<class PM, class ToPM grant_special_a ccess(To t, PM what)
{
// some fancy mechanism
// checking if 't' has access to _my_ privates
if (granted)
return what;
else
return 0;
}
};

Which actually looks like a security mechanism for safeguarding private
data members on per-instance basis. It's possible. Is it needed? Not
in our everyday programming activities. When is it needed? Well, when
some fancy system with inter-instance barriers is created... The language
does not have those things built-in. 'private' is not "per-instance", it
is "per-class". For whatever reason the OP thought otherwise.

Forming certain expectations before actually _learning_ the language, i.e.
coming to the language with preconceptions of how certain things should
behave, is often the cause of confusion and learning problems. One often
needs to "un-learn" things before one's ready to absorb real knowledge.
Nowadays we don't get asked often, but I remember when every fifth post
here was "what operator do I use to calculate Yth power of X? X^Y or
X**Y don't seem to work!"

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #6
In article <11************ **********@p79g 2000cwp.googleg roups.com>,
"Siam" <si*****@gmail. comwrote:
I read somewhere that private members of some instance of class A are
accessible from another instance of the same class. In other words,
access to private members is done 'on a class by class basis, not on an
instance by instance basis'. I couldn't quite believe it, and tried it
out for myself, and was shocked to see it was true...
What is the reasoning behind this?
The individual who wrote the class is in the best position to know if
one object of the class should be able to modify the data of another
object of the class.
Why should one Person object be able to see another Person object's
private parts?
Why shouldn't this be the case?
Aug 1 '06 #7

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

Similar topics

34
3116
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is this in any way used to create singletons. Can someone say how? Cheers, Andy
19
2351
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
10
2495
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
8
7832
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By scope they meant public/protected/private access modifiers :) Obviously all members of the base privately inherited class will be private, and that was my answer. However, the teacher checked my answers when I handed in, and the problem was that I had...
6
3592
by: z_learning_tester | last post by:
Quick question- What happens if you have a private class with a public static method? Can you still say the following? Lets say you are making this call from another class, say class2... int myVal = class1.method(); It seems that you should not be able to, after all from class2, you should not be able to see the private class1, so it's public static method is effectively wasted.
2
1829
by: Christoph Boget | last post by:
Let's take the following class: class MyClass { private int privateVar; public int PublicVar { get { return privateVar; } } public MyClass() {}
4
6306
by: newbie120 | last post by:
Hi all maybe its just been a long day, but i have a question about call access modifiers in C#. Consider the following code. namespace Application { private class Class1 { int i;
23
2611
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624. I read the description, decided that it's exactly what I want, and ignored the warning. Now I'm trying to inherit using a template. Instead of "destructor could not be generated because a base class destructor is inaccessible", I now have an...
14
4212
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10443
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6783
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.