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. How do I access private variable of an object ?

How do I access private variable of an object ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
15 Posts 6 Posters 2 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.
  • Mircea NeacsuM Mircea Neacsu

    Try dimensions.height() or dimensions.width()

    Mircea

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #3

    Well, that works but does not answer the question . I guess height and width are QRect public variables derived from "private" data. QRect doc should confirm that. I think I need to read-up on "friend" - that semms to be the real OOP ticket.

    Mircea NeacsuM D 2 Replies Last reply
    0
    • L Lost User

      Well, that works but does not answer the question . I guess height and width are QRect public variables derived from "private" data. QRect doc should confirm that. I think I need to read-up on "friend" - that semms to be the real OOP ticket.

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #4

      Member 14968771 wrote:

      I think I need to read-up on "friend" - that semms to be the real OOP ticket

      On the contrary: that might take you into a non-OOP design. The idea of using public member functions is to make your code independent of internal implementation details of other classes. In your case, a class representing a rectangle might keep a corner of the rectangle and its dimensions. Another one might keep two opposite corners or some other combination. If you try to access private members of the class your code will need to change when or if the implementation of the rectangle changes. If you stick to using only public interface, the internal details of the rectangle class may change but your code will remain the same. You cannot "buy" your way inside a class using a friend declaration. Just like in real life, the friendship is not a symmetrical relation. If you put a friend QRect declaration inside your class that doesn't give you any special access rights. It gives access rights to QRect to your class but obviously QRect is not going to use those rights. You would need to modify the QRect header file to insert a friend declaration mentioning your class. This is very intrusive and basically defeats the purpose of OOP design. I cannot make an OOP design course here, but there are some pretty good references available.

      Mircea

      1 Reply Last reply
      0
      • L Lost User

        Well, that works but does not answer the question . I guess height and width are QRect public variables derived from "private" data. QRect doc should confirm that. I think I need to read-up on "friend" - that semms to be the real OOP ticket.

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

        Member 14968771 wrote:

        I guess height and width are QRect public variables derived from "private" data.

        No, they are public methods that give you read-only access to private members.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        L 1 Reply Last reply
        0
        • L Lost User

          I have limited access to the object , but I can retrieve data of interest - in this case dimensions of one of the GUI objects.

          Form_Widget *FW = new Form_Widget(); // object
          QRect dimensions = FW->geometry(); // returns dimensions of specific part of the object
          int start_X = dimensions.x2; //can't do, QRect / dimensions are private

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          Why not study the documentation: QRect Class | Qt Core 5.15.4[^], which explains exactly how to get all the points of the rectangle?

          1 Reply Last reply
          0
          • L Lost User

            I have limited access to the object , but I can retrieve data of interest - in this case dimensions of one of the GUI objects.

            Form_Widget *FW = new Form_Widget(); // object
            QRect dimensions = FW->geometry(); // returns dimensions of specific part of the object
            int start_X = dimensions.x2; //can't do, QRect / dimensions are private

            R Offline
            R Offline
            RedDk
            wrote on last edited by
            #7

            One of the steps I invariably do, when debugging, is comment-out private blocks ... essentially rendering all statements public. Now that I get typing ... and that's just for starters. Without the essential error code (warning?) accompanying a successful compilation/link/running blank form ... there's little more time expendable for me, especially if you're already running debugger and just don't care to get descriptive enough with words and include a code. So, I'll just cut to the chase.

            1 Reply Last reply
            0
            • L Lost User

              I have limited access to the object , but I can retrieve data of interest - in this case dimensions of one of the GUI objects.

              Form_Widget *FW = new Form_Widget(); // object
              QRect dimensions = FW->geometry(); // returns dimensions of specific part of the object
              int start_X = dimensions.x2; //can't do, QRect / dimensions are private

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

              Hacking it.

              "In testa che avete, Signor di Ceprano?" -- Rigoletto

              In testa che avete, signor di Ceprano?

              L 1 Reply Last reply
              0
              • CPalliniC CPallini

                Hacking it.

                "In testa che avete, Signor di Ceprano?" -- Rigoletto

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #9

                Naughty.

                CPalliniC 1 Reply Last reply
                0
                • L Lost User

                  Naughty.

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

                  But true. :-D

                  "In testa che avete, Signor di Ceprano?" -- Rigoletto

                  In testa che avete, signor di Ceprano?

                  L 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    But true. :-D

                    "In testa che avete, Signor di Ceprano?" -- Rigoletto

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #11

                    Sorry, I posted this is wrong forum. But to the comedians - do not quit you day job. In the mean time I'll looking at usage of "friend".

                    L CPalliniC 2 Replies Last reply
                    0
                    • L Lost User

                      Sorry, I posted this is wrong forum. But to the comedians - do not quit you day job. In the mean time I'll looking at usage of "friend".

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      Member 14968771 wrote:

                      In the mean time I'll looking at usage of "friend".

                      Don't waste your time, since it will not make any difference. You cannot declare your class to be a friend of some other class in the hope of accessing its private variables. If that was possible then the millions of applications would be open to hacking. Do what I suggested above and make use of the information provided in the documentation: that is what it is there for..

                      1 Reply Last reply
                      0
                      • D David Crow

                        Member 14968771 wrote:

                        I guess height and width are QRect public variables derived from "private" data.

                        No, they are public methods that give you read-only access to private members.

                        "One man's wage rise is another man's price increase." - Harold Wilson

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #13

                        So my initial problem is I was accessing the private variable directly and not thru QRect. I am not sure I am saying it right. I'll take a look at QRect class. Thanks

                        1 Reply Last reply
                        0
                        • L Lost User

                          Sorry, I posted this is wrong forum. But to the comedians - do not quit you day job. In the mean time I'll looking at usage of "friend".

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

                          It was serious, actually. If you the designer of the class made a member private then you have two ways to access it: the publicly exposed interface (for instance the get/set methods) or a dirty hack on its bare representation in memory. Typically you cannot use friend because you are not the designer of the class.

                          "In testa che avete, Signor di Ceprano?" -- Rigoletto

                          In testa che avete, signor di Ceprano?

                          1 Reply Last reply
                          0
                          • L Lost User

                            I have limited access to the object , but I can retrieve data of interest - in this case dimensions of one of the GUI objects.

                            Form_Widget *FW = new Form_Widget(); // object
                            QRect dimensions = FW->geometry(); // returns dimensions of specific part of the object
                            int start_X = dimensions.x2; //can't do, QRect / dimensions are private

                            M Offline
                            M Offline
                            Magnus Forslund
                            wrote on last edited by
                            #15

                            Private members are only accessible via public functions. Try int start_X = QPoint.SetX(dimentions.x2);

                            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