Private member.
-
Hi I have one silly question. I have a class Shape { private: int x, y, z; public: void Get(Shape& s) { x = s.x; y = s.y; z = s.z; } }; This code compiles fine. But how? How can I access the private memebers s.x, s.y and s.z inside Get() even if it is member function of Shape. Both this and s are two different objects how can they access private members inside the class definition? if this happens then, int main() { Shape s, s1; s.x = s1.x; } this should also work, right? I am wondering why things are designed like this? Regards
The Best Religion is Science. Once you understand it, you will know God.
-
Hi I have one silly question. I have a class Shape { private: int x, y, z; public: void Get(Shape& s) { x = s.x; y = s.y; z = s.z; } }; This code compiles fine. But how? How can I access the private memebers s.x, s.y and s.z inside Get() even if it is member function of Shape. Both this and s are two different objects how can they access private members inside the class definition? if this happens then, int main() { Shape s, s1; s.x = s1.x; } this should also work, right? I am wondering why things are designed like this? Regards
The Best Religion is Science. Once you understand it, you will know God.
private effects members at the class level, not the object level. Remember, when you declared the members as private, there was no object involved :) When you are IN a member of the class (like in your Get() method) then you have access to all the private members of that class. From outside the class, like in your main function, you CANNOT access private members of the class, unless main() is a friend function of the class. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Hi I have one silly question. I have a class Shape { private: int x, y, z; public: void Get(Shape& s) { x = s.x; y = s.y; z = s.z; } }; This code compiles fine. But how? How can I access the private memebers s.x, s.y and s.z inside Get() even if it is member function of Shape. Both this and s are two different objects how can they access private members inside the class definition? if this happens then, int main() { Shape s, s1; s.x = s1.x; } this should also work, right? I am wondering why things are designed like this? Regards
The Best Religion is Science. Once you understand it, you will know God.
Private is a
class
scope (or meaning...) rule not an instance (or object) one. Hence what happens insideGet
method it is legal (an instance of the class can access private member of another instance of the same class). On the other handZainu wrote:
int main() { Shape s, s1; s.x = s1.x; }
it is INVALID because inside
main
you cannot access private member of (boths
ands1
) instances of theShape
class. Hope that helps. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
private effects members at the class level, not the object level. Remember, when you declared the members as private, there was no object involved :) When you are IN a member of the class (like in your Get() method) then you have access to all the private members of that class. From outside the class, like in your main function, you CANNOT access private members of the class, unless main() is a friend function of the class. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Hi Thank you very much for your time, But am not still fully convinced anyway. :confused: Regards
The Best Religion is Science. Once you understand it, you will know God.
Zainu wrote:
But am not still fully convinced anyway.
:) Once you wrap your head around it, you'll be ok :-D Here's another way to look at it. I'll break the Get() method out of the class declaration:
Shape
{
private:
int x, y, z;
public:
void Get(Shape& s);
};void Shape::Get(Shape& s)
{
x = s.x;
y = s.y;
z = s.z;
}Note that now I need a scope resolution operator ("::") on the Get() method implementation. Now you can clearly see that Get() is in the scope of the Shape class (I made it so by adding the "Shape::" scope resolution) so within the method you have access to the entire Shape class. Now, in your main() function:
int main()
{
Shape s, s1;
s.x = s1.x;
}Your main() function has NO scope resolution operator applied. So, it's definitely NOT in the scope of the Shape class, therefore no access to Shape class private or protected members is allowed. If this confused you more, then my work here is done ;) Mark "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Hi I have one silly question. I have a class Shape { private: int x, y, z; public: void Get(Shape& s) { x = s.x; y = s.y; z = s.z; } }; This code compiles fine. But how? How can I access the private memebers s.x, s.y and s.z inside Get() even if it is member function of Shape. Both this and s are two different objects how can they access private members inside the class definition? if this happens then, int main() { Shape s, s1; s.x = s1.x; } this should also work, right? I am wondering why things are designed like this? Regards
The Best Religion is Science. Once you understand it, you will know God.
Zainu wrote:
I am wondering why things are designed like this?
If things were not designed like this then how one can implement the copy constructor, assignment operator etc:-) One more pint if you remeber that if any member is used in a clss function the its gets prepent with this->. which is again the instance (object) of the class. These are the reason why things are designed in that way. So that private member of a object can be used directly when there are used in a member fucnction.
Manoj Never Gives up
-
Hi I have one silly question. I have a class Shape { private: int x, y, z; public: void Get(Shape& s) { x = s.x; y = s.y; z = s.z; } }; This code compiles fine. But how? How can I access the private memebers s.x, s.y and s.z inside Get() even if it is member function of Shape. Both this and s are two different objects how can they access private members inside the class definition? if this happens then, int main() { Shape s, s1; s.x = s1.x; } this should also work, right? I am wondering why things are designed like this? Regards
The Best Religion is Science. Once you understand it, you will know God.
Think of each class as a person. Think of each method and variable as something that person owns. You can assign many things different access modifiers: public, protected, private, ect. If you make an item you own(variable or method) private or protected you don't want any other person(class) using it right? Now say you want to be nice and share, make it public and then other people(class) can use it. Does that make it make more sense?
The only way to speed up a Macintosh computer is at 9.8 m/sec/sec.
-
Think of each class as a person. Think of each method and variable as something that person owns. You can assign many things different access modifiers: public, protected, private, ect. If you make an item you own(variable or method) private or protected you don't want any other person(class) using it right? Now say you want to be nice and share, make it public and then other people(class) can use it. Does that make it make more sense?
The only way to speed up a Macintosh computer is at 9.8 m/sec/sec.
ExpertComing wrote:
Does that make it make more sense?
Not even to those of us that are in the know.
ExpertComing wrote:
The only way to speed up a Macintosh computer is at 9.8 m/sec/sec.
This would look a lot better as: The only way to speed up a Macintosh computer is at 9.8 m/s2.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Hi I have one silly question. I have a class Shape { private: int x, y, z; public: void Get(Shape& s) { x = s.x; y = s.y; z = s.z; } }; This code compiles fine. But how? How can I access the private memebers s.x, s.y and s.z inside Get() even if it is member function of Shape. Both this and s are two different objects how can they access private members inside the class definition? if this happens then, int main() { Shape s, s1; s.x = s1.x; } this should also work, right? I am wondering why things are designed like this? Regards
The Best Religion is Science. Once you understand it, you will know God.
KISS. Internally a class must be able to access private members of an object of the same class so that it able to copy it or swap contents with it. That is you would find if very difficult (or impossible) to write copy constructors or “=” assignment operators if objects of the same class where not friends. The ‘main’ example you supplied is different because the assignment is being made outside of the class objects and from the out side there is no friendship. By the way, your ‘Get’ is an assignment (or copying) method and does not get anything.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra