Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用类计数器显示对象数_C++_Count - Fatal编程技术网

C++ 使用类计数器显示对象数

C++ 使用类计数器显示对象数,c++,count,C++,Count,我有一个类,可以用它创建许多对象 class Employee{ public: string firstName; string lastName; string gender;//gender - if necesary string identificationCode;//an Id for every employee string typeOfEmployee;//is Programmer, is Te

我有一个类,可以用它创建许多对象

class Employee{
public:
        string firstName;
        string lastName;
        string gender;//gender - if necesary
        string identificationCode;//an Id for every employee
        string typeOfEmployee;//is Programmer, is Tester and so on...

        string getFirstName(){ return this->firstName; } //show the name
        string getLastName(){ return this->lastName; }//show the name
        string getGender(){ return this->gender; }//show the gender - if necesary
        string getID(){ return this->identificationCode; }//show ID
        string getTypeOfEmployee(){ return this->typeOfEmployee; }//show type of job
};


class Programmer : public Employee{
public:
    vector<string> knownLanguages;//array of known programming languages
    unsigned int countProgrammers;
}programmer[5];

Programmer::Programmer()
{
countProgrammers++;
}

int main(){...

    switch (checkJob(getTokens[3]))
    {
    case 1:
        Programmer programmer[counter];//this is error = expression must be a value
        programmer[counter].identificationCode = getTokens[0];
        programmer[counter].firstName = getTokens[1];
        programmer[counter].lastName = getTokens[2];
        programmer[counter].typeOfEmployee = getTokens[3];
        programmer[counter].knownLanguagessplitString(getTokens[4],                 SecondDelimiter);

        //cout << programmer[counter].firstName<<" " << programmer[counter].lastName<<" with ID: " << programmer[counter].identificationCode<<" is a "<<programmer[counter].typeOfEmployee<<" and knows " << endl;
        counter++;
        break;


...}
class员工{
公众:
字符串名;
字符串lastName;
字符串性别;//性别-如果需要
string identificationCode;//每个员工的Id
字符串类型的员工;//是程序员,是测试人员等等。。。
字符串getFirstName(){返回this->firstName;}//显示名称
字符串getLastName(){返回此->lastName;}//显示名称
字符串getGender(){返回此->性别;}//显示性别-如果需要
字符串getID(){返回此->标识代码;}//显示ID
字符串getTypeOfEmployee(){返回此->typeOfEmployee;}//显示作业类型
};
类程序员:公共雇员{
公众:
vector knownLanguages;//已知编程语言的数组
无符号整数;
}程序员[5];
程序员::程序员()
{
编程人员++;
}
int main(){。。。
开关(checkJob(getTokens[3]))
{
案例1:
程序员[计数器];//这是错误=表达式必须是值
程序员[计数器].identificationCode=getTokens[0];
程序员[计数器].firstName=getTokens[1];
程序员[计数器].lastName=getTokens[2];
程序员[计数器].typeOfEmployee=getTokens[3];
程序员[计数器].knownLanguagessplitString(getTokens[4],第二个分隔符);

//cout您必须将变量声明为
static

static unsigned int countProgrammers;
然后在构造函数的主体中

Programmer::Programmer()
{
    ++countProgrammers;
}
这样,每次构造
程序员
时,该变量都将递增

要打印此值,您可以说

std::cout << Programmer::countProgrammers << std::endl;

std::这里需要注意的是:如果在析构函数中减少
countProgrammers
,请不要忘记复制构造函数,否则很快就会得到负值,请参见@Anedar。通常,保留这些静态计数器是一个糟糕的设计,尤其是当计数将用于ID或其他内容时。如果析构函数递减计数,多个实体很容易得到彼此相同的ID。感谢这个答案。现在困难的部分是,如果我想在主函数中打印它,我如何在类外使用它?@Justplayit,因为它既是
静态的
又是
公共的
,你可以简单地访问I作为
Programmer::countProgrammers
,请参阅我的编辑考虑到我已经使用
Programmer[5]
在类外创建了一个数据结构(我可以让5个程序员拥有自己的数据)