我的构造函数假设只有一个变量。但是我很好奇你是否可以初始化构造函数定义中没有的其他变量。构造函数在构造函数中设置的初始化和私有变量
class WordAnalysis{
private:
int timesDoubled;
word *words;
int wordCount;
int index;
void doubleArrayAndAdd(string);
bool checkIfCommonWord(string);
void sortData();
public:
bool readDataFile(char*); //returns an error if file not opened
int getWordCount();
int getUniqueWordCount();
int getArrayDoubling();
void printCommonWords(int);
void printResult(int);
WordAnalysis(int);
~WordAnalysis();
};
例如:WordAnalysis的任何实例现在都有timesdoubled为0,并且getter函数能够在没有setter的情况下获取此信息?
WordAnalysis::WordAnalysis(int arrSize){
wordCount = arrSize;
int timesDoubled = 0;
int index = 0;
}
我建议你拿起[一本好书](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)并开始阅读。 –