![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 3
![]() |
Question about passing objects into member functions
Let's assume I have the following code with the data members having relevant data.
class book {
int price;
char title[21];
public:
int comparePrice(book);
};
int main() {
book a1, b1; //assume a constructor set the variables to something
a1.comparePrice(b1);
return 0;
}
int book::comparePrice(book z) //returns 0 or 1
{
int rv;
if (price < z.price)
rv = 0;
else
rv = 1;
return rv;
}My question is since comparePrice is a member function I know it can access the object's price directly but doesn't the member function also become associated with a particular object of the class? What I mean is, how come my comparePrice() function call can access the private member of the passed book object if the function is a member of the a1 object? I hope the question is clear. This was an example from my class but I didn't have time to ask that question . |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
a1.comparePrice uses the a1 price; you just told it to. It compares it against the b1 price because you explicitly told it to (z.price, where z = b1).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Don't think of public/private on an object to object basis as much as a class to class basis.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Ahhhhh, I DID misunderstand the question.... The method gets passed an invisible pointer to the actual object; that's how it KNOWS which collection of members to use. Or, to put it another way, that's how it KNOWS which object it belongs to (and thus has access to).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
I believe what he is asking is, summarized:
"If a member is private in class X, then why can instance Y access said private member on instance Z?"
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Yeah, I finally got it. You said the answer. I don't believe I could phrase it better. Private members can be accessed from inside the bodies of methods of the class.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 3
![]() |
So essentially what you're saying is that the member functions of a class can access the private data member of another object of that class if the object is passed into the function. They are associated only at the class level and not necessarily at the object level. Is that right?
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The method is a member of the class. Each instantiation is an object. The method is sicced on the problem with a note in 'is hand about who he's working for, and a key to their house.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|