5 Replies - 25024 Views - Last Post: 26 August 2008 - 08:29 AM Rate Topic: -----

#1 BigPappa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-August 08

calling member functions from another class

Posted 25 August 2008 - 07:51 PM

Hi,
As an example, I have 5 classes like so...


class Game
{
	public
		void gameFuncA();
		void gameFuncB();

	private:
		Board m_Board;

};

class Player
{
	public:
		void doPlayerTurn();

		private:
				Hand playerHand
				std::vector<Hand > meldHand;


};

class Board
{
	public:
		void boardFuncA();
		void boardFuncB();
	
	private:
		std::vector<Player *> m_players;
};


class Hand
{
	public:
		void handFuncA();
		void handFuncB();

	private:
		vector<Cards > m_cards;
		SDL_Rect m_region;
};

class Cards
{
	public:
		void cardsFuncA();
		void cardsFuncB();

	private:
		int value;
		char suit;
		bool faceUp;
};




Now, please excuse me if this is confusing, which it is very hard for me to explain what I am looking for.

What I like to know is, with the above classes, how is it possible to call boardFuncA() inside of doPlayerTurn(), when the Player class and Board class are not instances or friends of each other?

Here is the reason why I am asking this.

I am planning on creating a 500 rummy game. My plan is to call the member function doPlayerTurn when it is the current player's turn. Inside of doPlayerTurn will be a loop that checks the mouse states through SDL. My problem is with the rules of when the player wants to pick up the cards from the waste pile. The rules say that the card that is being dragged from the waste pile must be used in a meld off of any of the players melding stacks. However, the problem is that I am in the current player class, and from my basic knoweldge of classes, you cannot access any of Board class member functions when you are inside Player class member function. So thats why I am asking if this is possible or is there a work around.

Is This A Good Question/Topic? 0
  • +

Replies To: calling member functions from another class

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: calling member functions from another class

Posted 25 August 2008 - 08:00 PM

put a reference (pointer) to the board inside of the player class.

Without having a reference to the object, for another object to invoke a member of another class that member would have to be static. (which is your other solution make boardFuncA() a static function, but that means that it can't actually manipulate any non-static data of any of the object instances.

This is actually a common sort of problem and there are various design patterns that offer solutions (depending upon the exact requirement) -- but in the end it probably boils down to -- give the player class a reference to the board class.

I can actually think of a few other ways if you really don't want to put a reference to the board inside the player class... So I would say exactly how you solve the problem would depend upon your model.
Was This Post Helpful? 0
  • +
  • -

#3 BigPappa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-August 08

Re: calling member functions from another class

Posted 25 August 2008 - 10:57 PM

View PostNickDMax, on 25 Aug, 2008 - 08:00 PM, said:

...

I can actually think of a few other ways if you really don't want to put a reference to the board inside the player class... So I would say exactly how you solve the problem would depend upon your model.


yes, iI kind of prefer not to include a reference of Board mostly because the Board class includes all of the players to start with. So in other words if the below example is the case....

There are 2 players and it is player1 turn. Now calling doPlayerTurn(m_board) will have 2 of the same player1 class (one coming from player1->doPlayerTurn and the from the Board class. If you can think of any other way, what I am looking for is 2 things.

1. When in the player class, this will handle the SDL mouse events and collect the pickup card and the drop off card.

2. The board class will control the rules of the game by checking if the current play is legal.

This post has been edited by BigPappa: 25 August 2008 - 10:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 BigPappa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-August 08

Re: calling member functions from another class

Posted 26 August 2008 - 07:53 AM

One thing I also forgot to mention is that I am using XCode 3.0 for Mac OS 10.5
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: calling member functions from another class

Posted 26 August 2008 - 08:08 AM

Bad practice, but what about a global board object?
Was This Post Helpful? 0
  • +
  • -

#6 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: calling member functions from another class

Posted 26 August 2008 - 08:29 AM

View PostBigPappa, on 26 Aug, 2008 - 01:57 AM, said:

View PostNickDMax, on 25 Aug, 2008 - 08:00 PM, said:

...

I can actually think of a few other ways if you really don't want to put a reference to the board inside the player class... So I would say exactly how you solve the problem would depend upon your model.


yes, iI kind of prefer not to include a reference of Board mostly because the Board class includes all of the players to start with. So in other words if the below example is the case....

There are 2 players and it is player1 turn. Now calling doPlayerTurn(m_board) will have 2 of the same player1 class (one coming from player1->doPlayerTurn and the from the Board class. If you can think of any other way, what I am looking for is 2 things.

1. When in the player class, this will handle the SDL mouse events and collect the pickup card and the drop off card.

2. The board class will control the rules of the game by checking if the current play is legal.


In any case, accessing a member function will require an instance of that class. There are many ways of hiding this fact, but everything will boil down to this. If you want to access a static function, then there won't need to be an instance.

One way of accessing the board without storing a pointer to it in each player is to make the Board a singleton. There are a few ways of doing this. One is to make all of the Board functions and variables static. Another way is to create a retrieval function that retrieves a global instance of the board. This can be done using a global variable, or a static variable within the Board class, and have a static function in the Board class retrieve it.

Or, as I think you might be getting at (but then again, I'm not sure if it's an example of what you don't want to do...):

void Player::doPlayerTurn(Board* board)
{
     board->boardFunc(/* Do something */);
}



This would be relatively easy to do, I think. So calling this from Game would be player->doPlayerTurn(&m_board);
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1