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