#ifndef SELF_SUM_H
#define SELF_SUM_H
#include<iostream>
unsigned int Sum_solution1(int n_value);
class SumDefaultConstruct{
private:
static unsigned int countN;
static unsigned int sumN;
public:
SumDefaultConstruct(){
countN++;
sumN+=countN;
}
static void reSet(){
countN=0;
sumN=0;
}
static unsigned int getSum(){
return sumN;
}
};
unsigned int SumDefaultConstruct::countN=0;
unsigned int SumDefaultConstruct::sumN=0;
unsigned int Sum_solution1(int n_value){
if(n_value==0){
return 0;
}
SumDefaultConstruct::reSet();
SumDefaultConstruct *ptr=new SumDefaultConstruct[n_value];
delete[] ptr;
ptr=NULL;
return SumDefaultConstruct::getSum();
}
#endif
unsigned int SumDefaultConstruct::countN=0;
unsigned int SumDefaultConstruct::sumN=0;
Used to be mandatory slightly these two sentences, for C + +, a period of time not to forget. static variables in C + + are only declared, but not defined. No storage space has been allocated, and static variables are generally about the same as global variables. Then we need to define or initialize the two static variables outside the class, that is, allocating space. Because a static variable belongs to all objects. The heart cannot be accessed with this because the object does not allocate storage space internally, but is in the global variable store.
From for notes (Wiz)
Static variables in constructors