首页 > 解决方案 > 为什么具有 2 个参数参数的构造函数接受复制对象作为 1 个参数参数?

问题描述

class person
{
    std::string name;
    int age;  
public:
    person(const std::string& name, int age) : name(name), age(age)
    {
    }
};

int main()
{
    person a("Bjarne Stroustrup", 60);
    person b(a);   // What happens here?
    b = a;         // And here?
}

为什么带有 2 个参数参数的构造函数接受复制对象作为参数。person b(a)我们用 1 个不同类型的参数调用构造函数,它有效吗?

如何 ?

标签: c++

解决方案



推荐阅读