derived class access

a simplified example. I made the variables protected so derived classes can access their own variables. I can't access a referenced object in the derived class method SecAttack. Do I have to make Enemy a friend to the base class Player? But then shouldn't I make the variables private?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Player
{
private:
	char *name;
protected:
	int health;
	int damage;
public:
	void attack(Player *prey)
	{
		prey->health-= damage;
	}
	//friend class Enemy;
};

class Enemy: public Player
{
public:
	void heal()		//self access test, ok
	{
		health+= 10;
	}
	void SecAttack(Player *Prey)	// cant access a Player object
	{
		Prey->health -= (damage+5);
	}
};


thanks
Last edited on
It looks like you are misunderstanding exactly what protected means.

When a derived class inherits protected methods and data it does not need to reference the class it inherited them from.

To get it to compile you need to change

1
2
3
4
        void SecAttack(Player *Prey)	// cant access a Player object
	{
		Prey->health -= (damage+5);
	}


to

1
2
3
4
        void SecAttack( )	// cant access a Player object
	{
	      health -= (damage+5);
	}
I don't remember. It seems that you can only access your protected members.

You could try a double-dispatch
1
2
3
4
	void SecAttack(Player *Prey)	// cant access a Player object
	{
		Prey->take_damage(damage+5);
	}
@IceThatJaw
im confused, the compiled one you gave me will attack itself?

ive read to use protected on variables for derived classes.

I want to access a object through the function SecAttack()
Last edited on

@IceThatJaw
im confused, the compiled one you gave me will attack itself?

ive read to use protected on variables for derived classes.

I want to access a object through the function SecAttack()


I answered your post too quickly.

You are trying to access a Player object when you should be accessing an Enemy object.
You can only access protected data within the structure or from a structure that is derived from it.

Just change void SecAttack(Player *Prey)
to
void SecAttack(Enemy *Prey)
then this wont work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Player
{
private:
	char *name;
protected:
	int health;
	int damage;
public:
	Player(char *n = "Player", int hp = 100, int dmg = 20)
		:name(n), health(hp), damage(dmg){}

	void attack(Player *prey)
	{
		prey->health-=damage;
	}
	friend int main();  //just to test
};

class Enemy: public Player
{
public:
	Enemy()
		:Player("Enemy",90,15){}

	void attack(Enemy *prey)
	{
		prey->health -= damage;
	}
	friend int main(); // just to test
};

void main()
{
	Player User("User",100,20);
	Enemy Troll;
	cout << User.health << endl;
	cout << Troll.health << endl;
	Troll.attack(&User);	// Error: player is incompatible
}


1
2
3
4
5
6
7
8
9
10
11
12
	class Player ...
	void takedmg(int dmg)
	{
		health-= dmg;
	}


	class Enemy ...
	void attack(Enemy *prey)
	{
		prey->takedmg(damage+5);
	}


this works, doesn't seem efficient though. the other alternative is to friend the derived class. whats the best alternative? is using friend allot bad?

thanks for your help
Last edited on
To be honest, your design is all messed up.

You are duplicating the method Attack when you don't have to. It would make sense if an Enemy's attack had different code because you could use polymorphism but in your case it is just redundant code.

If you want to get at things like health in your main function then you need to provide public getter methods that will return copies of them or just make make your class public or use a struct.
Topic archived. No new replies allowed.