[Newbie] Accessing mambers from other objects of the same class

E

Erik Wikström

Hello
I've recently started to learn C++ but I'm not new to
programming.
While reading the book "Teach Yourself C++ for Linux in 21 days"
from Sams I came across the following:
"Private data is available to the member functions of a class,
and different instances of the class can access each other's
data. In other words, if Frisky and Boots are both instances of
Cat, Frisky's member functions can access Frisky's data and also
Boots's data."

Having learned OO using Java I thought that this was quite
strang, should you be able to access private data from other
objects just because they are of the same type?

So I decided to test using the following code:

--------test.h
class tester {
public:
tester(int);
int getT() const;
void setOT(tester, int);
int getOT(tester) const;

private:
int T;
};

--------test.cpp
#include <iostream>
#include "test.h"

tester::tester(int t) {
T = t;
}

int tester::getT() const {
return T;
}

void tester::setOT(tester t,int n) {
cout << "Setting t.T to " << n << "...";
t.T = n;
cout << "Done!\n";
}

int tester::getOT(tester t) const{
cout << "t.T: " << t.T << endl;
}

--------t.cpp
#include <iostream>
#include "test.h"

int main() {
tester t1(5), t2(10);
cout << "t1: " << t1.getT() << endl;;
cout << "t2: " << t2.getT() << endl;;
t2.getOT(t1); // Try getting value of private member of other
object
t2.setOT(t1,0); // Try changing value of rivate member of
other object
t2.getOT(t1); // Tyr getting again
cout << "t1: " << t1.getT() << endl;;
cout << "t2: " << t2.getT() << endl;;
}

-------Output
t1: 5
t2: 10
t.T: 5
Setting t.T to 0...Done!
t.T: 5
t1: 5
t2: 10

I'm using gcc 2.95.4 running FreeBSD 4.8.

Now, the most supprising thing wasn't that it compiled without
any problem, but the fact that I could get values of other
objects private members but not set them. Is it supposed to be
this way and it's my interpretation of the text that is wrong or
should I be able to set the values too?
As things are now they make more sence from a OO point of view.
 
M

Mike Wahler

Erik Wikström said:
Hello
I've recently started to learn C++ but I'm not new to
programming.
While reading the book "Teach Yourself C++ for Linux in 21 days"
from Sams I came across the following:
"Private data is available to the member functions of a class,
and different instances of the class can access each other's
data. In other words, if Frisky and Boots are both instances of
Cat, Frisky's member functions can access Frisky's data and also
Boots's data."
Correct.

Having learned OO using Java I thought that this was quite
strang,

C++ is not Java.
should you be able to access private data from other
objects just because they are of the same type?

You'll have to decide what "should" means. This is the
way C++ works.
So I decided to test using the following code:

--------test.h
class tester {
public:
tester(int);
int getT() const;
void setOT(tester, int);
int getOT(tester) const;

I'm already beginning to supect what is confusing you.
Read on.
private:
int T;
};

--------test.cpp
#include <iostream>
#include "test.h"

tester::tester(int t) {
T = t;
}

int tester::getT() const {
return T;
}

void tester::setOT(tester t,int n) {

C++ passes (non-reference parameters) by value.
The parameter 't' is a *copy* of the object passed
as an argument. It will be destroyed upon exit
from this function. Changes to 't' will not
affect the caller's argument. This is not
specific to class member functions, but is
the behavior of all functions in C++.

If you want a function to modify its argument,
pass it by reference:

void tester::setOT(tester& t, int n)
{
t.T = n; /* will modify callers' argument */
}

cout << "Setting t.T to " << n << "...";
t.T = n;
cout << "Done!\n";
}

int tester::getOT(tester t) const{
cout << "t.T: " << t.T << endl;
}

--------t.cpp
#include <iostream>
#include "test.h"

int main() {
tester t1(5), t2(10);
cout << "t1: " << t1.getT() << endl;;
cout << "t2: " << t2.getT() << endl;;
t2.getOT(t1); // Try getting value of private member of other
object
t2.setOT(t1,0); // Try changing value of rivate member of
other object

As you had 'setOT()' defined, this will *not* modify the
object 't1', but a *copy of* it (the parameter) which was
created inside the function.

Change the parameter of 'setOT()' to a reference, and
you'll get the behavior you expect.
t2.getOT(t1); // Tyr getting again
cout << "t1: " << t1.getT() << endl;;
cout << "t2: " << t2.getT() << endl;;
}

-------Output
t1: 5
t2: 10
t.T: 5
Setting t.T to 0...Done!
t.T: 5
t1: 5
t2: 10

I'm using gcc 2.95.4 running FreeBSD 4.8.

Now, the most supprising thing wasn't that it compiled without
any problem,

Your code was 'correct' as far as the compiler is concerned,
it simply did not do what you expected, due to a misconception
(perhaps because you applied what you know about Java to C++
-- not a good idea to do this among *any* languages).
but the fact that I could get values of other
objects private members but not set them.

Your confusion is not with access rules, but parameter
passing.
Is it supposed to be
this way

Your program behaved correctly for the way you wrote it.
and it's my interpretation of the text that is wrong

Perhaps. I don't know that book, so I don't know.
Read about references, especially reference parameters.
or
should I be able to set the values too?

Not the way you wrote it. Use a reference.
As things are now they make more sence from a OO point of view.

What you seem to be confused about (parameter passing),
is not an OO issue.


-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top