Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Private member.

Private member.

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shiraztk
    wrote on last edited by
    #1

    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.

    M CPalliniC M E J 5 Replies Last reply
    0
    • S shiraztk

      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.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • S shiraztk

        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.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Private is a class scope (or meaning...) rule not an instance (or object) one. Hence what happens inside Get method it is legal (an instance of the class can access private member of another instance of the same class). On the other hand

        Zainu wrote:

        int main() { Shape s, s1; s.x = s1.x; }

        it is INVALID because inside main you cannot access private member of (both s and s1) instances of the Shape 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.

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • M Mark Salsbery

          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

          S Offline
          S Offline
          shiraztk
          wrote on last edited by
          #4

          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.

          M 1 Reply Last reply
          0
          • S shiraztk

            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.

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • S shiraztk

              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.

              M Offline
              M Offline
              Manoj Kumar Rai
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • S shiraztk

                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.

                E Offline
                E Offline
                Expert Coming
                wrote on last edited by
                #7

                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.

                D 1 Reply Last reply
                0
                • E Expert Coming

                  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.

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • S shiraztk

                    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.

                    J Offline
                    J Offline
                    John R Shaw
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups