473,770 Members | 5,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why C++ object can access its data member without via getter function??

here is a friend function of Class Array, which has two private data
member: int size, int *ptr

// Array's public member function to return size
int getSize() const { return size; }

friend istream &operator>>(ist ream &in, Array &a)
{
for(int i=0; i<a.size; i++)
// do something
return in;
}

here is my question: a is an object of Array, but in the for loop
condition, a access its private member size directly, instead of
calling getSzie(), like a.getSize(), does this violate the data
encapsulation principle? does anyone else(except me) think c++ is
stupid in some ways?

thanks a lot

Jul 23 '05 #1
6 4114
"blueblueblue20 05" <zh******@gmail .com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
here is a friend function of Class Array, which has two private data
member: int size, int *ptr

// Array's public member function to return size
int getSize() const { return size; }

friend istream &operator>>(ist ream &in, Array &a)
{
for(int i=0; i<a.size; i++)
// do something
return in;
}

here is my question: a is an object of Array, but in the for loop
condition, a access its private member size directly, instead of
calling getSzie(), like a.getSize(), does this violate the data
encapsulation principle? does anyone else(except me) think c++ is
stupid in some ways?

It does what you tell it to. If you want stupid things, do them, but
do not blame C++

I can only guess at the real code snippet we are talking about, but
'friend' means exactly what you are getting: access to the internal data
structures. Use it, if you absolutely must, otherwise, just keep away
from it.

hth
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #2
> does this violate the data encapsulation principle?
No.
does anyone else(except me) think c++ is stupid in some ways?

Of course C++ is stupid in some ways, but there's a difference between
stupid because it's stupid and stupid because you don't fully
understand the concept, or the intention of a feature.

Jul 23 '05 #3
right, totally agree with you, James!!

Jul 23 '05 #4
blueblueblue200 5 wrote:
here is a friend function of Class Array, which has two private data
member: int size, int *ptr

// Array's public member function to return size
int getSize() const { return size; }

friend istream &operator>>(ist ream &in, Array &a)
{
for(int i=0; i<a.size; i++)
// do something
return in;
}

here is my question: a is an object of Array, but in the for loop
condition, a access its private member size directly, instead of
calling getSzie(), like a.getSize(), does this violate the data
encapsulation principle?
Yes.
does anyone else(except me) think c++ is stupid in some ways?


You are the one who made it a friend, allowing it to access the private
data. If you think that is stupid, don't do it. C++ is specifically
designed to give you a lot of freedom, but with freedom always comes
responsibility.

Jul 23 '05 #5
if you don't like it , you can ignore as you consider.
but maybe one day you'll find such directl access
is usefully and powerfull. especially in C++, some other
rules are give to protecte from this kind of violation flooded.

C++'s other design principle is "it may let you do
in various way and technique"

you can use it,when you find that it's useful and flexilbe.

regards
Jul 23 '05 #6


blueblueblue200 5 wrote:
here is a friend function of Class Array, which has two private data
member: int size, int *ptr

// Array's public member function to return size
int getSize() const { return size; }

friend istream &operator>>(ist ream &in, Array &a)
{
for(int i=0; i<a.size; i++)
// do something
return in;
}

here is my question: a is an object of Array, but in the for loop
condition, a access its private member size directly, instead of
calling getSzie(), like a.getSize(), does this violate the data
encapsulation principle? does anyone else(except me) think c++ is
stupid in some ways?


In order to demonstrate a design flaw in C++ (or in any other
programming language), one needs an example of how this flaw affects
(or prevents the implementation of) a sound programming design. In this
case, the argument would be that C++'s support for friend declarations
makes it impossible to implement the solid design of class Array.

But is the Array class well designed? The first question that anyone
would ask, is why Array makes operator>> a friend, if operator>> can be
implemented using only those methods of A that are public? Why grant
operator>> access to A's private data members when no such access is
needed? Applying the principal of encapsulated design, it is clear that
Array should not grant such access to operator>>. So the case being
made: that C++'s support for friend declarations is "stupid", fails
because the Array class used as an example has a design flaw that no
one should want to implement - in C++ or in any other programming
language.

It is also possible to work from the other direction. Let's assume that
there is no support for friend declarations in C++. Access to classes'
private methods and data members would only be allowed within the
implementation of the class itself.

Now would it be possible to create a solid design for a program where
external access to a class's private data would be needed? The answer
would be "yes" and such cases can be found in the standard library
itself. There are many instances, particular in I/O operations, where
external access to a class's internal buffers is part of a solid
design.

Without C++'s support for friend declarations, those buffers would have
to be declared public - but to do so would violate encapsulation.
Keeping them private would prevent the implementation of a well
designed set of I/O operators. The solution? Of course it's the friend
declaration.

Far from being a defect of the language, C++'s friend declarations
allow programmers to implement solid designs that could not be
implemented without selective access to private data, or that could be
implemented only in ways that would violate the principles of
encapsulation.

Greg

Jul 23 '05 #7

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

Similar topics

12
2432
by: R | last post by:
Hello everybody. I'm writing my own Content System in PHP5. I've written so far main classes for handling DB connections, XML, XForms and Sessions. But I've got problem with one thing - it's not even relative with implementation - I'm looking for a smart solution My present system works like this:
1
2608
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for topics to include in a course on object-orientation that I'm going to conduct. (I will later summarize all the replies and discussion, for the
6
22535
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was 'myDog' without having to pass it as a parameter. //////////////////////////////// function...
3
24035
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked tables in the database where the code resides. If we move the database with the data tables to a new directory, the links are no longer valid. I tried to update the links by changing the Connect property and refreshing: Set td = db.TableDefs(0)...
11
3843
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
22
2778
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void fun() {
60
5062
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this. consistently may make the code easier to understand for someone else, etc. Using "this." makes it immediately clear, for example, that the variable being referred to is a member of the same class and is not declared in the method as a local variable. ...
73
4507
by: Water Cooler v2 | last post by:
I am new to PHP, just one day new. But I am otherwise a seasoned programmer on many other languages like C, VB 6, C#, VB.NET, Win32 platform and have some tiny bit experience in MFC, C++, Python. All the tutorials I read so far about PHP contain procedural examples. I haven't yet come accross a tutorial on the object oriented PHP. Could someone point me to one?
4
1770
by: tech | last post by:
Hi, I want to return a reference to member object provided in class Test, so have provided a getter function GetObj() and declared it const. However i'm getting a compiler error that i need to return reference to const obj but if i do do this i can't call non const member functions eg GetObj().NonConstFoo() How do i get round this problem. I woul think that GetObj is logically const as
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
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...
1
10001
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,...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
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();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
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.