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

Objects of the same class to access each other's private data

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 ) {cout<<"a.value="<<a.value<<endl; }
private:
int value;
};

There is of course the obvious solution to make the value public, but I
don't like that too much...

Cheers,
Manolis
Jul 23 '05 #1
12 2630
Why don't you just try to compile and run it?!

Jul 23 '05 #2
Yes, of course I've tried it; it doesn't work.

I just found a solution actually. Here it is (just in case someone else
wants it too):

class A {
public:
int add(const A& b) { return extaccess(*this,b); }
friend int extadd(const A& a, const A& b);
private:
int value;
};
int extadd( const A& a, const A& b )
{ a.value += b.value; return a.value; }

If someone has a nicer solution (apart from making the data public, or
having a public member to return the value) please let me know.

Manolis

__PPS__ wrote:
Why don't you just try to compile and run it?!

Jul 23 '05 #3
Manolis wrote:
Yes, of course I've tried it; it doesn't work.


It works fine for me, as it should. What was the error message that you got?

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #4
> 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 ) {cout<<"a.value="<<a.value<<endl; }
private:
int value;
};


This compiles fine:

#include <iostream>

class A
{
public:
void access( const A& a)
{
std::cout << "a.value=" << a.value << std::endl;
}

private:
int value;
};

It is legal for two A's to access their private members. What error do
you get?

Jonathan

Jul 23 '05 #5
Manolis wrote:
Yes, of course I've tried it; it doesn't work.

I just found a solution actually. Here it is (just in case someone else
wants it too):

class A {
public:
int add(const A& b) { return extaccess(*this,b); }
friend int extadd(const A& a, const A& b);
private:
int value;
};
int extadd( const A& a, const A& b )
{ a.value += b.value; return a.value; }

If someone has a nicer solution (apart from making the data public, or
having a public member to return the value) please let me know.

Manolis

__PPS__ wrote:
Why don't you just try to compile and run it?!


Class data is often private. When access to private data
is required the class will normally have get and/or set
methods for the private data. Why don't you want to
take that approach?

Here's a simple example using 'private' data:

class A1
{
public:
A1(int val) : m_x(val) {}

int x() const { return m_x; }

int add(const A1& oth)
{
m_x += oth.x();
return m_x;
}

private:
int m_x;
};
Here's another example using 'protected' data:

class A2
{
public:
A2(int val) : m_x(val) {}

int add(const A2& oth)
{
m_x += oth.m_x;
return m_x;
}

protected:
int m_x;
};

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #6
Larry I Smith wrote:
Manolis wrote:
Yes, of course I've tried it; it doesn't work.

I just found a solution actually. Here it is (just in case someone else
wants it too):

class A {
public:
int add(const A& b) { return extaccess(*this,b); }
friend int extadd(const A& a, const A& b);
private:
int value;
};
int extadd( const A& a, const A& b )
{ a.value += b.value; return a.value; }

If someone has a nicer solution (apart from making the data public, or
having a public member to return the value) please let me know.

Manolis

__PPS__ wrote:
Why don't you just try to compile and run it?!


Class data is often private. When access to private data
is required the class will normally have get and/or set
methods for the private data. Why don't you want to
take that approach?

Here's a simple example using 'private' data:

class A1
{
public:
A1(int val) : m_x(val) {}

int x() const { return m_x; }

int add(const A1& oth)
{
m_x += oth.x();
return m_x;
}

private:
int m_x;
};
Here's another example using 'protected' data:

class A2
{
public:
A2(int val) : m_x(val) {}

int add(const A2& oth)
{
m_x += oth.m_x;
return m_x;
}

protected:
int m_x;
};

Regards,
Larry


Excuse me...

I guess I'm brain-dead today.

The A1::x() method is only necessary to allow
code and classes that are not members of class A1
access to the value of the 'm_x' variable.

Members of the same class can access each other's
'private' data.

Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #7

Larry I Smith wrote:
Class data is often private. When access to private data
is required the class will normally have get and/or set
methods for the private data. Why don't you want to
take that approach?

Don't you personally think it's stupid to write all the empty
getters/setters? May be it's easier to make them public then (if access
to private data required)? Private data should be private, meaning
object uses it for it's own private need and the user handles objects
by methods of the obect which in some way interact with the private
data.
Here's a simple example using 'private' data:

Such simple example may lead to conclusion that private and protected
aren't that usefull and force programmers to write extra code :), just
try to add substruct, multiply, and others ...
class A1
{
public:
A1(int val) : m_x(val) {}

int x() const { return m_x; }

int add(const A1& oth)
{
m_x += oth.x();
return m_x;
}

private:
int m_x;
};
Here's another example using 'protected' data:

class A2
{
public:
A2(int val) : m_x(val) {}

int add(const A2& oth)
{
m_x += oth.m_x;
return m_x;
}

protected:
int m_x;
};

Looks so cool - private and protected, what's the point in these two
pieces of code?? didn't you just copy-second A1 and replaced private
for protected? :)
Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.


Jul 23 '05 #8

Manolis wrote:
Yes, of course I've tried it; it doesn't work.
It works. Maybe you had some other problems.
example:

class a{
public:
void doit(const a& x){
a *p1 = this, *p2 = &x;
std::cout << (p1->a+p2->a) << std::endl;
}
private:
int a;
};

how different are the two pointers p1 and p2? If you couldn't access
private members of an instance of the same class then p1 also woudn't
be allowed to access ->a. Offcource compiler could deduce for this
simple example that p1 allows this access but not p2, but with a more
complicated case it might be impossible to do. So, obects have access
to private/protected members of objects of the same class, otherwise it
would be impossible to use p1->a in my example. All this is possible
because access rights are compile time checked.
I just found a solution actually. Here it is (just in case someone else wants it too):

class A {
public:
int add(const A& b) { return extaccess(*this,b); }
friend int extadd(const A& a, const A& b);
private:
int value;
};
int extadd( const A& a, const A& b )
{ a.value += b.value; return a.value; }

If someone has a nicer solution (apart from making the data public, or having a public member to return the value) please let me know.

Manolis

__PPS__ wrote:
Why don't you just try to compile and run it?!


Jul 23 '05 #9
> Larry I Smith wrote:
Class data is often private. When access to private data
is required the class will normally have get and/or set
methods for the private data. Why don't you want to
take that approach?
Don't you personally think it's stupid to write all the empty
getters/setters?


Be careful when you make statements such as "this is stupid".
May be it's easier to make them public then (if access
to private data required)? Private data should be private, meaning
object uses it for it's own private need and the user handles objects
by methods of the obect which in some way interact with the private
data.
No. Data should be private when it may be dangerous for the objet's
state to modify it without validation. What's more, using member
functions to access an aggregated object separates implementation from
interface. If the aggregated objet changes, the "accessor" may be able
to make it transparent. Allowing clients to use what is called
"implementation details" makes the object harder to modify.

I personally never use public data, except for very simple cases (such
as a point class). Using member functions allows me to control the
access and to separate between interface and implementation.
Here's a simple example using 'private' data:


Such simple example may lead to conclusion that private and protected
aren't that usefull and force programmers to write extra code :),

just try to add substruct, multiply, and others ...


More complex examples will not add anything to the problem and will
more probably confuse the original poster. Well-written, simple
examples, even if they don't depict the whole spectrum of the language,
are usually easier to comprehend.

This is for Larry I Smith:
class A1
{
public:
A1(int val) : m_x(val) {}

int x() const { return m_x; }

int add(const A1& oth)
{
m_x += oth.x();
This is unecessary. Different objects of the same class may access
their private and protected members directly.
return m_x;
}

private:
int m_x;
};

Here's another example using 'protected' data:
This example does not explain the use of protected data nor the
interaction between two objects using this features (such as a derived
class). I also do not understand why your first example uses a member
function to access 'm_x' and the following one does not.
class A2
{
public:
A2(int val) : m_x(val) {}

int add(const A2& oth)
{
m_x += oth.m_x;
return m_x;
}

protected:
int m_x;
};

Jonathan

Jul 23 '05 #10

Jonathan Mcdougall wrote:
Larry I Smith wrote:
Class data is often private. When access to private data
is required the class will normally have get and/or set
methods for the private data. Why don't you want to
take that approach?
Don't you personally think it's stupid to write all the empty
getters/setters?


Be careful when you make statements such as "this is stupid".


Sorry if it hurt you, I just remembered reading someone else's code
where for evry private member the author (probaly using macroses in
his/her text editor) put correstonding T getXXX(){return XXX;} void
setXXX(newXXX){XXX=newXXX;}. I think that person read somewhere that
it's good to have data members private :)
May be it's easier to make them public then (if access
to private data required)? Private data should be private, meaning
object uses it for it's own private need and the user handles objects
by methods of the obect which in some way interact with the private
data.


No. Data should be private when it may be dangerous for the objet's
state to modify it without validation. What's more, using member
functions to access an aggregated object separates implementation

from interface. If the aggregated objet changes, the "accessor" may be able to make it transparent. Allowing clients to use what is called
"implementation details" makes the object harder to modify.
I think you rephrased my short sentence about "Private data should be
private...". Imagine, the agregated obect is some sort of private data,
and the object that stores it provides some methods that in some way
iteract (or don't) with it's private data... I really didn't understand
some parts of your message about separated interface and
implementation, and transparent chage of aggregate...

I personally never use public data, except for very simple cases (such as a point class). Using member functions allows me to control the
access and to separate between interface and implementation.
Me too. I don't remeber when I used public data :)
A good example for public data would be complex numbers, where two
doubles (x,i) could be directly accessed. It would make sence to make
setters/getters for the two doubles only in case when we need to
calculate absolute value of the complex very often; In such case we
would have private member that stores absolute value and whenever x or
i gets new value we recalculate absolute value...
not sure if I'm right with termins. I meant absolute(complex) =
sqrt(x*x + i*i);

Here's a simple example using 'private' data:
Such simple example may lead to conclusion that private and protected
aren't that usefull and force programmers to write extra code :),

just
try to add substruct, multiply, and others ...


More complex examples will not add anything to the problem and will
more probably confuse the original poster. Well-written, simple
examples, even if they don't depict the whole spectrum of the

language, are usually easier to comprehend.

Well, example with complex numbers is really easy and tells where we
have benifit of using setters and private data.... Not only it has real
meaning (not like the A1 and A2 classes) but it's also very short
example.
This is for Larry I Smith:
class A1
{
public:
A1(int val) : m_x(val) {}
int x() const { return m_x; }

int add(const A1& oth)
{
m_x += oth.x();
This is unecessary. Different objects of the same class may access
their private and protected members directly.
return m_x;
}

private:
int m_x;
};

Here's another example using 'protected' data:
This example does not explain the use of protected data nor the
interaction between two objects using this features (such as a

derived class). I also do not understand why your first example uses a member function to access 'm_x' and the following one does not.
class A2
{
public:
A2(int val) : m_x(val) {}

int add(const A2& oth)
{
m_x += oth.m_x;
return m_x;
}

protected:
int m_x;
};

Jonathan


Jul 23 '05 #11
Jonathan Mcdougall wrote:
Larry I Smith wrote:


This is for Larry I Smith:
class A1
{
public:
A1(int val) : m_x(val) {}
int x() const { return m_x; }
int add(const A1& oth)
{
m_x += oth.x();
This is unecessary. Different objects of the same class may access
their private and protected members directly.
return m_x;
}
private:
int m_x;
};
Here's another example using 'protected' data:


This example does not explain the use of protected data nor the
interaction between two objects using this features (such as a derived
class). I also do not understand why your first example uses a member
function to access 'm_x' and the following one does not.


See my 2nd post (9 minutes after the original) - because I was
'brain-dead' and responded too quickly without thinking it through.
In my head I was thinking of derived classes, but I didn't
put that in my post.

Some days I shouldn't get out of bed...

Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #12
You are absolutely right. It works (today)! I must have been
'brain-dead' too, last night; I was getting some error, which probably
was irrelevant, and I thought that this was the problem (but I have a
tiny bit of an excuse: my class is quite big; I hadn't actually compiled
the simple one I sent to the newsgroup).

Anywayz. Thanks everybody for your replies.

Regards,
Manolis

PS: I didn't want to make it public or add functions to access/modify
the value, since only members of this class should know about (and thus
access/modify) the value.
Jonathan Mcdougall wrote:
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 ) {cout<<"a.value="<<a.value<<endl; }
private:
int value;
};

This compiles fine:

#include <iostream>

class A
{
public:
void access( const A& a)
{
std::cout << "a.value=" << a.value << std::endl;
}

private:
int value;
};

It is legal for two A's to access their private members. What error do
you get?

Jonathan

Jul 23 '05 #13

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

Similar topics

49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
6
by: Garma | last post by:
According to what I have learnt so far, instantiating global objects should be the last resort. Is there any reasons why so? Sometimes some objects or their pointers have to be shared among...
106
by: xtra | last post by:
Hi Folk I have about 1000 procedures in my project. Many, many of them are along the lines of function myfuntion () as boolean on error goto er '- Dim Dbs as dao.database Dim Rst as...
0
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico...
14
by: tshad | last post by:
I have people telling me that I should set up objects for my tables, but I am finding the Null problem makes that difficult. It isn't a big problem if you are not updating the table, but if you...
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...
31
by: attique63 | last post by:
how could I write a program that will declare a class point. The class point has two private data members x and y of type float. The class point has a parameterized constructor to initialize both...
1
by: Nemisis | last post by:
hi guys, Currently converting an old classic asp system to a OOP asp.net application. We are building the new application using a 3 tier arcitecture and i was wondering about the following. ...
6
by: Guy Thornton | last post by:
I have an application in asp.net using vb. The asp.net portion of this application is mainly the user interface. The UI has references made to our business logic layer. And the business logic...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.