我在与模板,由此,如果你试图给一个模板函数的字符串参数,编译器的“Hello World”作为为const char [12]的问题。我希望它是const char *。自动转换为const char []为const char *的模板函数
我可以通过静态铸造每个字符串为“为const char *”“解决”的问题,而是因为我想用这个作为一个记录系统的一部分,使其成为简单是一个很大的目标。
自很难解释我的意思,我想出了一个简单的再现。你会看到主函数的最后一行不能编译。
任何帮助,将不胜感激
#include <string>
// Trivial base class so we can use polymorphism
class StoreItemsBase
{
public:
StoreItemsBase() {}
};
// Example of a trivial Templated class to hold some 3 items.
// Intent to have similar classes to hold 4,5..n items
template <typename T1, typename T2, typename T3>
class Store3Items : public StoreItemsBase
{
public:
Store3Items(const T1& t1, const T2& t2, const T3& t3)
:
StoreItemsBase(),
mT1(t1),
mT2(t2),
mT3(t3)
{}
private:
T1 mT1;
T2 mT2;
T3 mT3;
};
// Function to create a pointer to our object with added id
// There would be similar CreateHolderFunctions for 4,5..n items
template <typename T1, typename T2, typename T3>
StoreItemsBase* CreateHolder(const T1& t1, const T2& t2, const T3& t3)
{
return new Store3Items<T1, T2, T3>(t1, t2, t3);
}
int main()
{
int testInt=3;
double testDouble=23.4;
const std::string testStr("Hello World");
StoreItemsBase* Ok1 = CreateHolder(testInt, testDouble, testStr);
StoreItemsBase* Ok2 = CreateHolder(testDouble, testStr, testInt);
StoreItemsBase* Ok3 = CreateHolder(testStr, static_cast<const char*>("Hello there"), testInt);
// If you try a standard string, it compiler complains
// Although I could surround all my strings with the static cast, what I am looking for is a way
// to for the CreateHolder function to do the work for me
StoreItemsBase* NotOk4 = CreateHolder(testStr, "Hello World", testInt);
// Free our objects not shown in the example
}
编译错误是: example.cpp:在构造“Store3Items :: Store3Items(常量T1 &,常量T2 &,常量T3 &)[用T1 = std :: basic_string,T2 = char [12],T3 = int]':T1351 = std :: basic_string,T2 = char [12],T3 = int]' exampl e.cpp:65:74:从这里实例化 example.cpp:21:11:error:用作初始化程序的数组
const char []与const char *不同。尽管直接投射可能是隐含的,但反过来却不是。这就像是想要将一个父类投给它的孩子。 –
一般来说,请包含编译器错误信息 - 即使专家将能够更快地回答您的问题。 – ndkrempel
P.S. char []和char *具有不同的语义 - 是否要Store3Items存储字符串的副本或指向原始字符串的指针? – ndkrempel