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. Accessing private class members

Accessing private class members

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
8 Posts 4 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.
  • G Offline
    G Offline
    georgiek50
    wrote on last edited by
    #1

    I have a class structure forming a linked list where there is another class (declared as private within the main class) such as class Link { private: class LinkElement { friend class Link... ... Is it possible to access the PRIVATE members of the LinkElement class from a function that is not part of either of the two classes? I am trying to go through the linked list in a thread and trying to set various pointers to both the class Link and LinkElement but can't get access to that member. Is this possible or am I wasting my time?

    L I J 3 Replies Last reply
    0
    • G georgiek50

      I have a class structure forming a linked list where there is another class (declared as private within the main class) such as class Link { private: class LinkElement { friend class Link... ... Is it possible to access the PRIVATE members of the LinkElement class from a function that is not part of either of the two classes? I am trying to go through the linked list in a thread and trying to set various pointers to both the class Link and LinkElement but can't get access to that member. Is this possible or am I wasting my time?

      L Offline
      L Offline
      loket
      wrote on last edited by
      #2

      #define private public :cool: Or cast (LinkElement *)(classpointer + whatoffestis) Or make the accessing class a friend or make som accessor functions. /M


      - Don't sweat the petty things, and don't pet the sweaty things.

      G 1 Reply Last reply
      0
      • L loket

        #define private public :cool: Or cast (LinkElement *)(classpointer + whatoffestis) Or make the accessing class a friend or make som accessor functions. /M


        - Don't sweat the petty things, and don't pet the sweaty things.

        G Offline
        G Offline
        georgiek50
        wrote on last edited by
        #3

        Thanks for the speedy reply, but I'm a bit confused with your answer. Here is what compiles and what doesn't for me cLink::cLinkElement *pPointer; (compiles with the class warning that it's not initialized to anything) now doing this: pPointer->pNextLink = NULL; (it shows up fine on class view btw but I get this compilation error: error C2248: 'pNextLink' : cannot access private member declared in class 'cLink::cLinkElement' Is there anyway to get around this by not making the privates publics?

        1 Reply Last reply
        0
        • G georgiek50

          I have a class structure forming a linked list where there is another class (declared as private within the main class) such as class Link { private: class LinkElement { friend class Link... ... Is it possible to access the PRIVATE members of the LinkElement class from a function that is not part of either of the two classes? I am trying to go through the linked list in a thread and trying to set various pointers to both the class Link and LinkElement but can't get access to that member. Is this possible or am I wasting my time?

          I Offline
          I Offline
          Ian Darling
          wrote on last edited by
          #4

          You do get what private means, right? It's an implementation detail that you shouldn't rely on, because somebody is guaranteed to change it the moment you start monkeying about with it. Ignore the suggestion of #define private public (although made in jest), because that's the sort of thing that shots you in the foot later on. You can fudge a solution by declaring an identical class/struct (with the same compiler packing options, etc!), but without the use of private. If you're lucky, the compiler will generate matching object layouts, and you can reinterpret_cast a pointer from one type to the other, and then access the private members through that. Use of this technique is extremely brittle, and if I was your boss or peer, I'd be distinctly unimpressed with you, and make you junk that code. Generally speaking, what you're trying to do indicates you don't understand the problem correctly, you don't understand the language properly, or you have a crap design that needs rethinking. And if this is homework, then go back to the textbooks :-)


          Ian Darling "The different versions of the UN*X brand operating system are numbered in a logical sequence: 5, 6, 7, 2, 2.9, 3, 4.0, III, 4.1, V, 4.2, V.2, and 4.3" - Alan Filipski

          G 1 Reply Last reply
          0
          • I Ian Darling

            You do get what private means, right? It's an implementation detail that you shouldn't rely on, because somebody is guaranteed to change it the moment you start monkeying about with it. Ignore the suggestion of #define private public (although made in jest), because that's the sort of thing that shots you in the foot later on. You can fudge a solution by declaring an identical class/struct (with the same compiler packing options, etc!), but without the use of private. If you're lucky, the compiler will generate matching object layouts, and you can reinterpret_cast a pointer from one type to the other, and then access the private members through that. Use of this technique is extremely brittle, and if I was your boss or peer, I'd be distinctly unimpressed with you, and make you junk that code. Generally speaking, what you're trying to do indicates you don't understand the problem correctly, you don't understand the language properly, or you have a crap design that needs rethinking. And if this is homework, then go back to the textbooks :-)


            Ian Darling "The different versions of the UN*X brand operating system are numbered in a logical sequence: 5, 6, 7, 2, 2.9, 3, 4.0, III, 4.1, V, 4.2, V.2, and 4.3" - Alan Filipski

            G Offline
            G Offline
            georgiek50
            wrote on last edited by
            #5

            You are right on the issue that I have crap design, but this is no means homework. I have been programming c++ for about 2 years, completely self taught, and learning by my mistakes as I go along, since I have no one to teach me and I am not doing this for a living. I have found a workaround to this by not declaring that member private anymore. My understanding of private is that it's a member of a class that can be accessed only by the functions of that class and/or the class object, correct? Also, although your criticism is appreciated, and I know I am a really bad coder (should have seen me 2 years ago!!!!) a little help would be appreciated to go along with the criticism.

            I 1 Reply Last reply
            0
            • G georgiek50

              You are right on the issue that I have crap design, but this is no means homework. I have been programming c++ for about 2 years, completely self taught, and learning by my mistakes as I go along, since I have no one to teach me and I am not doing this for a living. I have found a workaround to this by not declaring that member private anymore. My understanding of private is that it's a member of a class that can be accessed only by the functions of that class and/or the class object, correct? Also, although your criticism is appreciated, and I know I am a really bad coder (should have seen me 2 years ago!!!!) a little help would be appreciated to go along with the criticism.

              I Offline
              I Offline
              Ian Darling
              wrote on last edited by
              #6

              georgiek50 wrote: a little help would be appreciated to go along with the criticism. Point taken :-) What exactly are you trying to achieve? I might be able to provide more constructive guidance with a brief outline of the task you're trying to solve.


              Ian Darling "The different versions of the UN*X brand operating system are numbered in a logical sequence: 5, 6, 7, 2, 2.9, 3, 4.0, III, 4.1, V, 4.2, V.2, and 4.3" - Alan Filipski

              1 Reply Last reply
              0
              • G georgiek50

                I have a class structure forming a linked list where there is another class (declared as private within the main class) such as class Link { private: class LinkElement { friend class Link... ... Is it possible to access the PRIVATE members of the LinkElement class from a function that is not part of either of the two classes? I am trying to go through the linked list in a thread and trying to set various pointers to both the class Link and LinkElement but can't get access to that member. Is this possible or am I wasting my time?

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

                I have read some of the replies and the best way to learn is by doing. If you wish to access a private memvebers of a class from a function that is not part of the class, you need to do one of three things: Provide member functions in your class to access the private member variables (preferded method), declare your function(s) as a friend of the class (this says the function knows what it is doing), or make the member data public (this is normaly a bad idea). I recommend the books by Bruce Eckel for learning more about the C++ language itself, although they concern the langauge and not any specific OS. Good luck, and have fun! P.S. I love programming! INTP

                G 1 Reply Last reply
                0
                • J John R Shaw

                  I have read some of the replies and the best way to learn is by doing. If you wish to access a private memvebers of a class from a function that is not part of the class, you need to do one of three things: Provide member functions in your class to access the private member variables (preferded method), declare your function(s) as a friend of the class (this says the function knows what it is doing), or make the member data public (this is normaly a bad idea). I recommend the books by Bruce Eckel for learning more about the C++ language itself, although they concern the langauge and not any specific OS. Good luck, and have fun! P.S. I love programming! INTP

                  G Offline
                  G Offline
                  georgiek50
                  wrote on last edited by
                  #8

                  Thanks to everyone for their replies. Although I don't think that books can help me out any more because I am stubborn and never learn until I make the mistake. John's comment about creating another member function to get the details from the private members sounds the best way (don't know why I didn't think of it myself...been at it for 10 hours today, probably that's why!) Thanks, again.

                  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