简体   繁体   English

如何在类周围共享参数-C ++

[英]How to share parameters around a class - C++

I have a class which I intend to use for performing calculations. 我有一个打算用于执行计算的类。 I declare one static object of the class elsewhere which I use to obtain results. 我在其他地方声明了该类的一个静态对象,用于获取结果。 The class has one public function apart from the constructor. 除了构造函数之外,该类还具有一个公共函数。

public:
    double getProbability(PlayerStats &p1, PlayerStats &p2, Score &score);

As you can see, I have three objects as input parameters. 如您所见,我有三个对象作为输入参数。 My class then has two private functions which are called from getProbability() . 然后,我的班级有两个私有函数,它们从getProbability()调用。 One requires p1 and p2 , the other requires all three of the parameters 一个要求p1p2 ,另一个要求所有三个参数

My question is this. 我的问题是这个。 Is it better to pass these objects as parameters to the function or is it better to create private member variables and use these. 将这些对象作为参数传递给函数是否更好,还是创建私有成员变量并使用它们更好。

So for example 所以举个例子

   double MyClass::getProbability(PlayerStats &p1, PlayerStats &p2, Score &score){
            otherFunction(p1,p2);
            anotherFunction(p1,p2,score);
             ....
    }

or 要么

     double MyClass::getProbability(PlayerStats &p1, PlayerStats &p2, Score &score){
            this->p1 = p1;
            this->p2 = p2;
            this->score = score;
            otherFunction();   //use member variables in these functions
            anotherFunction();
             ....
    }       

It's better to pass them as parameters, as they don't really represent class state and storing them as members will probably be more performance costly than simply continuing to pass around the references. 最好将它们作为参数传递,因为它们实际上并不表示类状态,而将它们存储为成员可能比继续传递引用更昂贵。

But it doesn't really look like your class has any state at all, which leads me to believe all of these methods should be free-functions in a suitable namespace rather than members of a class (perhaps you come from a Java background?). 但这似乎并不真正表明您的类完全没有任何状态,这使我相信所有这些方法应该是在适当的命名空间中的自由函数,而不是类的成员(也许您来自Java背景?) 。

I prefer passing the parameter to the private functions. 我更喜欢将参数传递给私有函数。 As far as your class may be access by multiple concurrent threads, it is better to send each data to its related function. 只要您的类可以被多个并发线程访问,最好将每个数据发送到其相关函数。

You should pass the arguments directly to your other methods. 您应该将参数直接传递给其他方法。 Even if other functions used them it would be very confusing because nothing in the function signature indicates that those arguments would be kept around. 即使其他函数使用了它们,也会造成混乱,因为函数签名中没有任何内容表明这些参数将被保留。

If you need some of these parameters in other functions and those parameters remain the same through several calls, you could refactor your class. 如果在其他函数中需要其中一些参数,并且这些参数通过多次调用保持不变,则可以重构类。 It would take the ones which stay the same in the constructor and then function calls just pass in the ones which will change. 它需要那些在构造函数中保持不变的函数,然后函数调用才传入那些将改变的函数。 It really depends on the needs of your application to determine whether this is a better approach. 实际上,这取决于您的应用程序需求,以确定这是否是一种更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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