John Goche写道: 您好, 请考虑以下示例。有一个函数 foo(Foo1< Foo2foo1){...} void foo(Foo1< Foo2> ;& r_foo1){} > 类Foo1有一个空构造函数。 你的意思是默认的ctor。 这没关系,Foo1需要一个模板参数而没有人知道 如果在其模板列表中提供任何默认值。 > Foo2可以从Foo3的实例foo3。 我可以致电: foo(foo3); 仅当以下内容有效时: Foo1< Foo2foo1foo2; Foo3实例( foo1foo2); 根据上述关于Foo3的陈述,这是不太可能的。 尝试: Foo1< Foo3 foo1foo3; foo(foo1foo3); 我遇到了类似的问题。 我有我的List< template>,如果模板类是''Car'' 并且无法传递List< Ferrarito我的函数,就像你 试图。我必须创建一个名为Recast的新功能。 在这种情况下,我使用rtti函数动态投射它,(检查 如果从法拉利车到汽车是安全的,如果 就抛出一个例外它们是不可能的。你可以把它剪掉,但是如果说你把汽车送到法拉利,就会发生不好的事情。 / *! *这用于重铸整个列表,因为元素可以互相继承 。仅限Win32。 * / 模板< class NewElementType> 列表< NewElementType>& 重播( ){ //我们应该知道,如果ElementType和NewElementType都是 兼容 ElementType tempT; NewElementType&安培; tempNT = dynamic_cast< NewElementType&>(tempT); // 如果我们要转换为错误的数据类型,则强制运行时抛出异常 List< NewElementType> * recast =(List< NewElementType> *)这个; 返回*重新制作; } John Goche写道: 您好, 请考虑以下示例。有一个函数 foo(Foo1< Foo2foo1){...} 类Foo1有一个空构造函数。 Foo2可以用Foo3的foo3实例构建。 我可以打电话: foo( foo3); ? 谢谢, JG Hello, Consider the following example. There is a function foo(Foo1<Foo2foo1) { ... } The class Foo1 has an empty constructor. Foo2 can be constructed from an instance foo3 of Foo3. Can I call: foo(foo3); ? Thanks, JG 解决方案 John Goche wrote:Hello,Consider the following example. There is a functionfoo(Foo1<Foo2foo1) { ... }The class Foo1 has an empty constructor.Foo2 can be constructed from an instance foo3 of Foo3.Can I call:foo(foo3);?The parameter of the function foo is of type Foo1<Foo2>. The object youpass when you call foo must therefore be of type Foo1<Foo2or of sometype that can be converted to Foo1<Foo2>. So the only question thatmatters is, can an object of type Foo3 be converted to type Foo1<Foo2>.Unless the answer is yes, your code will not compile. The fact that a Foo2 can be constructed from a Foo3 is irrelevant. Thefunction foo does not take a Foo2, it takes a Foo1<Foo2>, which is acompletely different type. In types that might be more familiar: if Foo1 is std::vector, Foo2 isstd::string and Foo3 is const char*, you have void foo(std::vector<std::stringv) { ... } and you are trying to do foo("some string literal"); A std::vector<std::stringcan not be constructed from a string literalso the code will not compile. A std::string can be constructed from astring literal, but that is irrelevant because we are not trying toconstruct a std::string, we are trying to construct astd::vector<std::string>. Gavin Deane John Goche wrote:Hello,Consider the following example. There is a functionfoo(Foo1<Foo2foo1) { ... }void foo(Foo1< Foo2 >& r_foo1) { } >The class Foo1 has an empty constructor.You mean a default ctor.That doesn''t matter, Foo1 takes a template parameter and nobody knowsif any default(s) is/are providied in its template list. >Foo2 can be constructed from an instance foo3 of Foo3.Can I call:foo(foo3);only if the following is valid: Foo1<Foo2foo1foo2;Foo3 instance( foo1foo2 ); Which according to the above statement about Foo3, is rather unlikely. Try: Foo1< Foo3 foo1foo3;foo( foo1foo3 ); I had a similar problem. There I had my List<template>, and in case of template class were ''Car''and could not pass a List<Ferrarito my function, just like you''retrying to. I had to make a new function, called "Recast". In this case, I used an rtti function to dynamically cast it, (checkingif it''s safe to cast from Ferrari to Cars, throwing an exception ifthey can''t be casted). You can cut it off, but bad things can happen ifyou cast from, like saying, Cars to Ferraris. /*!* This is used to recast the whole list because elements can inheriteach other. Win32 only.*/template<class NewElementType>List<NewElementType>&Recast() {// we should be aware if both ElementType and NewElementType arecompatibleElementType tempT;NewElementType& tempNT = dynamic_cast<NewElementType&>( tempT); //force runtime to throw exception if we''re casting to wrong datatype List<NewElementType>* recast = (List<NewElementType>*) this;return *recast;}John Goche wrote:Hello,Consider the following example. There is a functionfoo(Foo1<Foo2foo1) { ... }The class Foo1 has an empty constructor.Foo2 can be constructed from an instance foo3 of Foo3.Can I call:foo(foo3);?Thanks,JG 这篇关于施工问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-25 00:30