How I prevent having too much parameters in a function. C++ [closed]

爷,独闯天下 提交于 2021-02-11 15:39:35

问题


I am making a game engine with C++ and Opengl. I making a small game to test my incomplete engine. But after adding classes like Camera, Renderder, Level, Player, Zombie and Human, My Player class's function parameters getting long about 8 arguments because player needs to interact with many classes like Camera, Renderder, Zombie, Human, etc.

Please give suggestions to solve this problem Or this is the right way to make games.


回答1:


Difficult to say without more context. However, one extreme is to bundle everything needed as parameter into a single struct:

 struct GameContext {
      Camera camera;
      Renderer renderer;
      Level level;
      Player player;
      //...
 };

and then pass that as single parameter:

 void foo(const GameContext& context);

However, whether this is feasible depends not only on the function that you want to pass that structure as parameter. Rather you have to consider more than that to decide what belongs together and what not. Sometimes the other extreme (passing 8 individual parameters) can be the right thing. If that function is the only place where those 8 parameters appear together, then you would not put them in a struct merely to call that one function.

To summarize, I think you are trying to fix something at the wrong abstraction level. Consider the whole architecture and how your objects are supposed to interact with each other. A Camera or a Renderer is not something I would expect to be passed around that often. You need to tell a Player only once what Camera they should use (if at all) not every time you call one of Players methods.




回答2:


First, objects that are single inside a scene, can be implemented via singleton pattern, then you would be able to access them from any class simply by adding a required header.

Second, some of the objects can be represented as parents for others - like Legs or Hands for Human, make use of composition.

Third, as most interactions between objects (like collisions and stuff) are common between all objects, you can leave most of the implementation in the base class, which would free Zombies from accessing Camera at all, because Object already does that. Make use of inheritance!

Fourth, if nothing worked, you can group up them all within a struct of pointers to these objects, and pass a single struct instead of N parameters.

Pretty much that.




回答3:


This is hard to answer with without seeing some of the code, but here are some thoughts:

  • just because your player needs to interact with many classes doesn't mean they all have to appear in the same function call. In fact, if "camera" and "zombie" appear in the same function call, I suspect your functions are not very clear in their purpose. Each function should ideally do one thing, and get only the parameters it needs.
  • if you don't have that already, try to find appropriate base classes - e.g., your player class might interact with an instance of a Monster class, which might either be a Zombie, or a Vampire, or a Werewolf.
  • if you still find your parameter lists to be too long to handle, find objects that live on the same level of abstraction, or in the same domain, and put them together into aggregate objects that you hand into your functions. For example, camera and renderer could go into "viewer", whereas monsters, non-player characters, and items could be contained in a "level" or a "world".


来源:https://stackoverflow.com/questions/61390127/how-i-prevent-having-too-much-parameters-in-a-function-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!