5 Replies - 221 Views - Last Post: 13 May 2013 - 03:49 PM Rate Topic: -----

#1 Zarthax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-May 13

How to allow main and other classes access to same instance of class?

Posted 12 May 2013 - 04:46 PM

What I am trying to do is just create a simple battle between the player and an enemy. Now, I've got most of the basics of C++ down, the main thing is I'm not quite sure HOW exactly to do it. Here's the problem, I have two classes, player and enemy. What I want to know is, how do I allow both main and each class have access to the same instance of the other class? Since main needs to be able to call, say, the attack function of player (therefore needing an object to be declared), then player needs to be able to access the health and defense of enemy (needing to have access to the same object that will be declared in main). When enemy attacks, it will require the same of player. I'm confused on how to do this properly, as the solution I came up with didn't really hold up. I have searched for examples and even looked at certain tutorials trying to find something that would answer my question, but this is kind of a specific question. So a tutorial won't answer it, and every dang code example I come across isn't sophisticated enough to address the problem at all, they're just too basic. Hopefully I have given enough information for an answer, I don't think I need any code as it's not really that type of a question. However I will post the code if absolutely necessary.

Is This A Good Question/Topic? 0
  • +

Replies To: How to allow main and other classes access to same instance of class?

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,575
  • Joined: 19-February 09

Re: How to allow main and other classes access to same instance of class?

Posted 12 May 2013 - 05:31 PM

You can extract data and input data to the instances using member functions.

Hero hero;
Monster monster;

bool fighting = true;
while (fighting)
{
  monster.take_hit( hero.make_hit() );
  hero.take_hit( monster.make_hit() );

  if( monster.dead() || hero.dead() ) 
  {
    fighting = false;
  }
}


Was This Post Helpful? 1
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,575
  • Joined: 19-February 09

Re: How to allow main and other classes access to same instance of class?

Posted 12 May 2013 - 06:04 PM

You can also use pointers and references to refer to the same object.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3553
  • View blog
  • Posts: 11,014
  • Joined: 05-May 12

Re: How to allow main and other classes access to same instance of class?

Posted 12 May 2013 - 07:51 PM

Reading the following made me very nervous:

View PostZarthax, on 12 May 2013 - 07:46 PM, said:

then player needs to be able to access the health and defense of enemy (needing to have access to the same object that will be declared in main). When enemy attacks, it will require the same of player.


If you give the player access to the health and defense of the enemy (and vice versa) you would be breaking the law of Demeter. Read this article as to why that would be bad: http://www.ccs.neu.e...boy/demeter.pdf

#define gave you the perfect pseudo-code of what needed to happen which maintains the correct principles.
Was This Post Helpful? 0
  • +
  • -

#5 Zarthax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-May 13

Re: How to allow main and other classes access to same instance of class?

Posted 13 May 2013 - 02:35 PM

So then, would this code be correct?:

enemyObj.setHealth(playerObj.getAttack());


The enemy setHealth function will take the attack of player from getAttack and minus it from the enemy's health, would that violate the law of demeter?
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,575
  • Joined: 19-February 09

Re: How to allow main and other classes access to same instance of class?

Posted 13 May 2013 - 03:49 PM

A fight involves at least two characters, so it is not a part of one character. The fight function (or class) will involve data from both classes.


It is unlikely, that one object would set the health of another, one object may affect the health of another though.

enemyObj.removeXHitPointsFromHealth(playerObj.getOneHitOfAnAttack());




Could be done like :

int hitpoints = playerObj.getOneHitOfAnAttack();
enemyObj.removeXHitPointsFromHealth(hitpoints);



So one object affects another, just how, depends on your exact requirements.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1