【发布时间】:2015-08-28 15:37:45
【问题描述】:
#include <string>
#include <iostream>
#include <vector>
class HasPtrValue {
public:
HasPtrValue(const HasPtrValue& h): ps(new std::string(*h.ps)) { std::cout << "copy" << std::endl;}
HasPtrValue(const std::string &s = std::string()): ps(new std::string(s)) { std::cout << "string/default" << std::endl;}
~HasPtrValue() { delete ps; }
private:
std::string *ps;
};
using namespace std;
int main(){
string s = "stackoverflow";
vector<HasPtrValue> a(5, s);
}
以上代码编译好输出:
string/default
copy
copy
copy
copy
copy
这表明向量首先使用字符串对象(执行 HasPtrValue(s))直接初始化一个临时 HasPtrValue 对象,然后从这个临时对象中复制构造 5 个元素。那么,下面的代码怎么编译不了:
int main(){
vector<HasPtrValue> a(5, "stackoverflow");
}
如果它直接初始化 HasPtrValue(执行 HasPtrValue("stackoverflow")),那么 const string& 构造函数承担创建临时的角色就没有问题。我得到了错误;
error: no matching function for call to 'std::vector<HasPtrValue>::vector(int, const char [14])'|
我想我会尝试一个更简单的类,它使用一个 int 构造函数并从一个 double 转换:
class A{
public:
A(const int& a): x(a) { }
int x = 2;
};
int main(){
vector<A> a(5, 5.5);
}
除了这个编译得很好。向量实现的哪一部分阻止在构造函数中使用 const char* 转换?
【问题讨论】:
-
答案已经给出。只需添加:
HasPtrValue(const char* s) : ps(new std::string(s)) { std::cout << "string/constcharptr" << std::endl; }
标签: c++ vector constructor