accessing protected stuff

This is a discussion on accessing protected stuff within the C++ Programming forums, part of the General Programming Boards category; This is a general outline of my code. I am wondering why I have trouble accessing the protected members as ...

  1. #1
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61

    accessing protected stuff

    This is a general outline of my code. I am wondering why I have trouble accessing the protected members as such. Please let me know if what I have written is not enough info:

    Code:
    struct node
    {
       node* child
       //...
    }
    
    struct node2
    {
        node2* sibling
        //...
    }
    
    class one
    {
        public:
             void a();
        protected:
             node* current;
    };
    
    class two : public one
    {
         public:
             void b()
         private:
             node2* current2;
    };
    
    .
    .
    .
    
    void two::b()
    {
         //the linked list is NULL for functions in class two but in class one it is complete and works as expected.
         if(current->child != NULL) // never  proceeds through the if statement when I want it too...
         {
               //stuff
               a();
          }
         else
          {
              return;
           }
    }
    Last edited by lord; 10-19-2008 at 09:20 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    ? You don't have a variable named node in class two. Even if you meant current2, that doesn't have a link field, just a field (confusingly) named current2.

  3. #3
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    tabstop: what I am trying to do is have access to the node in class one...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't have a variable named node in class one either.

  5. #5
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    sorry, I fixed it... Do you know what I am trying to do now? sorry about that

  6. #6
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    So what I am trying to do is not only have access to the protected stuff in class one but have all the values remain intact when using them from class two.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, every object of class two has its own current; it doesn't share one with some particular instance of class one.

  8. #8
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    How can I allow class two to share current with class one?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, but that doesn't really make any sense. Every object of class one has its own current too. If I did this:
    Code:
    one a, b, c, d;
    two e, f, g, h;
    I'd have eight different currents running around (I'd have a.current, and b.current, and ..., and h.current). What sense does it make to talk about sharing?

  10. #10
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    The second class builds a linked list that depends on how the first class created its own linked list. I do not want the second class to view it as NULL...

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why are you using a variable of class one at all, then? Just use a variable of class two the whole time. (There may be slicing issues, I suppose, but you can always pass by pointer if you have to.) Or if for some reason you want e to see a's linked list, then set e.current=a.current and be done with it. (Edit: of course, then any change made in e will be seen in a and vice versa.)
    Last edited by tabstop; 10-19-2008 at 10:07 PM.

  12. #12
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    The linked lists have different designs. But are you saying move current from class one into class two? Then the functions from class one do not have access to it...

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't need to move current from class one to class two -- it's already there. I'm guessing, based solely on what you seem to want from how I read your posts, that you should not have any variables of type one at all. You get a variable of type two; when you need the first kind of linked list you use current, when you need the second kind of linked list you use current2, and that's it.

  14. #14
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    I see what your saying (I think)... For example, class one has a function called a() that creates the first linked list, I just create an object from class two (call it e) and call e.a() .... then e.b() (b is a function of class two) will have the first linked list values intact...

    right?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's right.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do u access a class protected area?
    By CwannaB in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 07:52 AM
  2. Protected Inheritance
    By golfinguy4 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2002, 09:56 AM
  3. Your stuff
    By smog890 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 11:50 PM
  4. Porting to protected mode
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-04-2001, 03:53 PM
  5. Protected...?
    By face_master in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2001, 10:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21