这是参数还是类型?

时间:2018-11-06 21:15:45

标签: c++ templates

在下面的示例代码中,我想知道传递给getSocialSecurityNumber类中的Citizen的参数是什么。为什么是(Passkey<Government>)而不是(Passkey<Government> x)

它与类模板/函子有关吗?

#include <string>
#include <iostream>

using namespace std;


template <typename T>
class Passkey {
private:
    friend T;
    Passkey() {}
    Passkey(const Passkey&) {}
    Passkey& operator=(const Passkey&) = delete;
};

class Citizen {
public:
    string getSocialSecurityNumber(Passkey<Government>) const { return _socialSecurityNumber; };
                                   ^^^^ (here)

private:
    string _socialSecurityNumber;
};

class Government {
private:
    void printCitizenInfo(const Citizen &citizen) const;
};

void Government::printCitizenInfo(const Citizen &citizen) const {
    cout << "Citizen SSN: " << citizen.getSocialSecurityNumber(Passkey<Government>()) << endl;
}

1 个答案:

答案 0 :(得分:3)

Passkey<Government>是一种类型。该参数显然未使用,它没有名称。