By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,377 Members | 1,658 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,377 IT Pros & Developers. It's quick & easy.

Using protected methods within a derived class?

P: n/a
This doesn't seem to make any sense to me.

I have three classes, the declarations of which are shown below:

class Action
{
public:
virtual void Act (int range, int elevation, Combatant* actor ) = 0;
virtual void React (Action* incoming, Combatant* actor ) = 0;
virtual string Name ( ) const = 0;
virtual bool CanAct (int range, int elevation, Combatant* actor ) =
0;
virtual void Act (int range, int elevation, Combatant* actor,
Target& target ) = 0;
virtual bool CanAct (int range, int elevation, Combatant* actor,
Target& target ) = 0;
virtual ~Action() = 0;
};

class AttackAction : public Action
{
protected:
virtual void CalculateSpecials (Score* attack, Score* defense ) =
0;
virtual void CalculateDamage (Score* attack, Score* defense ) = 0;
virtual void DoDamage (int attacker, int defender ) = 0;
virtual ~AttackAction() = 0;
};

class SpecialAttack : public AttackAction
{
public:
virtual void Act (int range, int elevation, Combatant* actor );
virtual void React (Action* incoming, Combatant* actor );
virtual string Name ( ) const;
virtual bool CanAct (int range, int elevation, Combatant* actor );
virtual void Act (int range, int elevation, Combatant* actor,
Target& target );
virtual bool CanAct (int range, int elevation, Combatant* actor,
Target& target );
virtual ~SpecialAttack();

protected:
AttackAction* m_decorated;

virtual void CalculateSpecials (Score* attack, Score* defense );
virtual void CalculateDamage (Score* attack, Score* defense );
virtual void DoDamage (int attacker, int defender );
};

Now, inside any of the protected methods of SpecialAttack, if I try
this:

m_decorated->CalculateSpecials(attack, defense);

The compiler spits back the error:
error: 'virtual void
Battle::AttackAction::CalculateDamage(Battle::Scor e*, Battle::Score*)'
is protected
specialattack.cpp:20: error: within this context

Is this something that I've done wrong, or is it a bug within the
compiler? It seems intuitively obvious to me that if any of those
methods are protected, I should be able to use them within a derived
class.

Thanks for your help.

May 29 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Tigera wrote:
This doesn't seem to make any sense to me.

I have three classes, the declarations of which are shown below:

class Action
{
public:
virtual void Act (int range, int elevation, Combatant* actor ) = 0;
virtual void React (Action* incoming, Combatant* actor ) = 0;
virtual string Name ( ) const = 0;
virtual bool CanAct (int range, int elevation, Combatant* actor ) =
0;
virtual void Act (int range, int elevation, Combatant* actor,
Target& target ) = 0;
virtual bool CanAct (int range, int elevation, Combatant* actor,
Target& target ) = 0;
virtual ~Action() = 0;
};

class AttackAction : public Action
{
protected:
virtual void CalculateSpecials (Score* attack, Score* defense ) =
0;
virtual void CalculateDamage (Score* attack, Score* defense ) = 0;
virtual void DoDamage (int attacker, int defender ) = 0;
virtual ~AttackAction() = 0;
};

class SpecialAttack : public AttackAction
{
public:
virtual void Act (int range, int elevation, Combatant* actor );
virtual void React (Action* incoming, Combatant* actor );
virtual string Name ( ) const;
virtual bool CanAct (int range, int elevation, Combatant* actor );
virtual void Act (int range, int elevation, Combatant* actor,
Target& target );
virtual bool CanAct (int range, int elevation, Combatant* actor,
Target& target );
virtual ~SpecialAttack();

protected:
AttackAction* m_decorated;

virtual void CalculateSpecials (Score* attack, Score* defense );
virtual void CalculateDamage (Score* attack, Score* defense );
virtual void DoDamage (int attacker, int defender );
};

Now, inside any of the protected methods of SpecialAttack, if I try
this:

m_decorated->CalculateSpecials(attack, defense);

The compiler spits back the error:
error: 'virtual void
Battle::AttackAction::CalculateDamage(Battle::Scor e*, Battle::Score*)'
is protected
specialattack.cpp:20: error: within this context

Is this something that I've done wrong, or is it a bug within the
compiler?
The former.
It seems intuitively obvious to me that if any of those
methods are protected, I should be able to use them within a derived
class.
You can, but only on the same object.

May 29 '07 #2

P: n/a
Now, inside any of the protected methods of SpecialAttack, if I try
this:

m_decorated->CalculateSpecials(attack, defense);

The compiler spits back the error:
error: 'virtual void
Battle::AttackAction::CalculateDamage(Battle::Scor e*, Battle::Score*)'
is protected
specialattack.cpp:20: error: within this context

Is this something that I've done wrong, or is it a bug within the
compiler? It seems intuitively obvious to me that if any of those
methods are protected, I should be able to use them within a derived
class.
The protected members of any class can be accessed by friends of the
class or pointer/reference to the class derived from it. Although you
have derived publicly from class AttackAction but you are invoking it
from a pointer to AttackAction. What you are trying to do is akin to
this

class A
{
protected:
int i;
};

A* ptr;
ptr->i; //not allowed as i is protected.

Try invoking CalculateSpecials directly from within SpecialAttack
(which will eventually result in this->CalculateSpecials ) and it
should compile fine.

May 29 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.