Thread: Accessing a protected member declared in parent class

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

    Accessing a protected member declared in parent class

    Hi,

    I have a base class character, a child class pacman, and a file called manager.cpp which contains main and serves to manage the characters in the game while the game is in progress.

    In Character.h I have declared desiredDirection and its mutators and accessors as follows:
    Code:
    protected:
    	void setDirection(int newDirection);
    	const int getDirection() const;
    	void setDesired(int newDirection); //set desired direction
    	const int getDesired() const;
                          ...
    
    private:
                          ...
    	int direction; //current direction
    	int desiredDirection;
                          ...
    In manager.cpp I have declared pacman to be a global variable as follows (I know global variables are naughty but I haven't thought of the best way to manage the characters yet so its temporary):
    Code:
    #include <stdlib.h>
    #include <stdarg.h>
    #include <stdio.h>
    #include <math.h>
    #include <string> //might be able to remove later - here for debugging
    
    #include "Map.h"
    #include "Pacman.h"
    #include "Ghost.h"
    
    using namespace std;
    
    Pacman * pacman;
    in the same file (manager.cpp), I need to access pacman's desiredDirection and I do it as follows:
    Code:
    void Timer(int extra)
    {
    	if (pacman->getDesired() == 4)
    		pacman->move();
    	else
    	{
    		pacman->move();
    		pacman->setDesired(pacman->move(pacman->getDesired()));
    	}
    	glutTimerFunc(30,Timer,0);
    }
    before I had this working by having desiredDirection as a global variable in Manager.cpp but I'm trying to avoid using global variables. When I compile my program I get:

    manager.cpp(43) : error C2248: 'Character::getDesired' : cannot access protected member declared in class 'Character'
    character.h(38) : see declaration of 'Character::getDesired'
    character.h(15) : see declaration of 'Character'

    I'm guessing the only way I can use the getters and setters is if I used them within pacman's implementation file? So, if I need access in manager.cpp, what would be the best way to do it?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  3. #3
    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.

  4. #4
    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