Thread: Accessing a protected member declared in parent class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    Your getters and setters shouldn't be protected, but public. (If they are protected, then no one can access them, which means they are extremely pointless.)
    Well, technically, the class itself, and any that inherits from it can access protected members (be it functions or variables). This make protected in itself fairly pointless, since you can either not inherit the class anyways - in which case they may as well be private, or you if you want to access the member, you just inherit the class into your own derived class and have your merry way with whatever member it is you want to "play" with.

    But for the usage described above, the setter/getter members need to be made public to make them work.

    [And of course, ANY of the members are really accessible to anyone who can see the class variable (or guess where it is), since there is always the possibility of casting it into a pointer that you can access the memory of the object with - but that's sort of cheating ]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Talking

    thanks I got so lost in playing with inheritance within the pacman class that I lost sight of the fact that it's totally normal to have mutators and accessors be public haha. thanks for bringing me back to earth :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cannot access private member declared in class
    By newme in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2008, 03:57 PM
  2. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM