简体   繁体   中英

Passing many variables from a function in one class to a function in another class

I am creating a chess engine using bitboards and I have two classes: class Board and class Evaluation .

There are many variables in the Board class that I want to pass to a function in the Evaluation class. This class evaluates the position of the Board and returns a number. I want to pass all my bitboards to this class. They are around 15 in number and may even increase.

I will be creating an object of the Evaluation class in the Board class and from the Board class I will be calling the boardEvaluation function. It is to this function that I want to pass these bitboards.

One way to do this is to simply accept 15 arguments in the function I want to pass the variables to. However, I would prefer not using this approach since it does not sound appealing.

Is there any other way to pass so many variables from one class to a function in another class?

Create another class BoardData and use it in both Board and Evaluation . The responsibility of BoardData would be to store your 15 variables - and maybe provide some basic operations on them. In this way your design will be simplified without circural depependencies.

Here are the steps:

  1. boardEvaluation function can have Board* as it's argument
  2. The function being friend of the Board class.
  3. Then in the Evaluation.h header file forward declare the Board class
  4. Use the 15 private member variables of Board class directly in the function.

Having said that, when the design comes to the point that you are about to use friend you probably need to reconsider your original design and relationships between the two classes. In your case, you may probably need to move the boardEvaluation to the Board class instead, if possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM