c++ - Inheritance understanding access denied for a derived class -


i little bit stumped on @ moment, understand, specific enemies inherit base enemy class, when created use base enemy constructor, create own objects copies of variables work with. when use inherited enemy's set_e function wound`t set own e_damage , e_health work with? , if why denying access own variables?

in locals tab on visual basic each x object indeed have own enemy variables, if try direct access enemy class access error, if try use objects own error, doing wrong?

base enemy class

class enemy { public:   enemy(); ~enemy();   void set_e(float, float); float get_e_h(); float get_e_d();   virtual void battle() = 0;   float* e_damage; float* e_health; char* e_descrip; float* e_attack;  }; 

enemy distructor

   enemy::~enemy()   {     if (e_damage != nullptr)   {     delete e_damage;     e_damage = nullptr;   }   if (e_health != nullptr)   {         delete e_health;     e_health = nullptr;   }   if (e_attack != nullptr)   {     delete e_attack;     e_attack = nullptr;   }    if (e_descrip != nullptr)   {     delete e_descrip;     e_descrip = nullptr;   }   } 

enemy constructor:

e_damage = new float; e_damage = nullptr;  e_health = new float; e_health = nullptr;  e_attack = new float; e_attack = nullptr; 

example inherited enemy.h

class t_rex : private enemy, private cs_text_adventure { public:  void t_rex::set_e(float dmg, float health, t_rex x);  t_rex();   ~t_rex();   void battle() override;  }; 

example inherited enemy.cpp

void t_rex::battle() {     t_rex x;      x.set_e(15.0f, 100.0f, x);      e_descrip = "the passage way opens tropical forrest";          std::cout << std::endl;         std::cout << x.e_descrip << std::endl;         std::cout << std::endl;         while(*fight == true)         {             *x.e_attack = rand() % 100 + 0;              if(*x.e_attack >= 30)             {                 *p_health -= *x.e_damage;             }             else if(*x.e_attack < 30)             {                 *x.e_health -= *p_health;             }             if(*p_health <= 0)             {              *gamerun = false;             }             else if(*e_health <= 0)             {             *fight = false;             }         }         step++; } 

unauthorized access on pointer deference reassign:

void t_rex::set_e(float dmg, float health, t_rex x) { *x.e_damage = dmg; *x.e_health = health; } 

lets take these 2 lines constructor:

e_damage = new float; e_damage = nullptr; 

first allocate single float, , make e_damage point it. make e_damage null pointer, no longer points anywhere valid. leads memory leak.

later use null pointer, leads undefined behavior , possible crashes.

for single values there's seldom need use pointers @ all, in case. declare e_damage (and other member variables) normal non-pointer variables.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -