简体   繁体   中英

Initialize variables in a class method?

I have a silly question:

I have a class and in one of its methods I have to do some calculations and I need to declare some variables that are useful to do this. For example:

class Percettrone {
public:
    Percettrone(double NL);
    void fit(double X, double y);
}
.
.
.
void Percettrone::fit(double X, double y)
{
        double delta[50][50];
        double conteggio=0;
        //calculations
}

Is it correct? Or is it better to declare when I create the class? Moreover I have to call this method lots of time during my programme

If those variables are affecting the object instance and identifying its state, then you must declare them in the class as attributes. On the other hand, if they're just helper variables in the function, and you don't need to use them anywhere else, then make them local inside the function as you did in the example.

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