施工问题 [英] construction question

查看:62
本文介绍了施工问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


请考虑以下示例。有一个函数


foo(Foo1< Foo2foo1){...}


类Foo1有一个空构造函数。


Foo2可以用Foo3的foo3实例构建。


我可以打电话:


foo( foo3);





谢谢,


JG

解决方案



John Goche写道:


你好,


请考虑以下示例。有一个函数


foo(Foo1< Foo2foo1){...}


类Foo1有一个空构造函数。


Foo2可以用Foo3的foo3实例构建。


我可以打电话:


foo( foo3);





函数foo的参数是Foo1< Foo2>类型。因此,当你调用foo时,你传递的对象必须是Foo1< Foo2or类型的某些

类型,可以转换为Foo1< Foo2>。所以

重要的唯一问题是,Foo3类型的对象是否可以转换为类型Foo1< Foo2> ;.

除非答案是肯定的,否则您的代码将无法编译。


Foo2可以用Foo3构建的事实无关紧要。

函数foo不接受Foo2,它需要一个Foo1< Foo2> ;,这是一个完全不同类型的




在可能更熟悉的类型中:如果Foo1是std :: vector,则Foo2是

std :: string而Foo3是const char *,你有


void foo(std :: vector< std :: stringv){...}


你想要做的事


foo(some string literal);


std :: vector< std :: string不能用字符串文字构造

所以代码不会编译。 std :: string可以用

字符串文字构造,但这是无关紧要的因为我们没有尝试构造一个std :: string
,我们正在尝试构建一个

std :: vector< std :: string> ;.

Gavin Deane



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 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);

?

The parameter of the function foo is of type Foo1<Foo2>. The object you
pass when you call foo must therefore be of type Foo1<Foo2or of some
type that can be converted to Foo1<Foo2>. So the only question that
matters 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. The
function foo does not take a Foo2, it takes a Foo1<Foo2>, which is a
completely different type.

In types that might be more familiar: if Foo1 is std::vector, Foo2 is
std::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 literal
so the code will not compile. A std::string can be constructed from a
string literal, but that is irrelevant because we are not trying to
construct a std::string, we are trying to construct a
std::vector<std::string>.

Gavin Deane



John Goche wrote:

Hello,

Consider the following example. There is a function

foo(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 knows
if 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''re
trying to. I had to make a new function, called "Recast".

In this case, I used an rtti function to dynamically cast it, (checking
if it''s safe to cast from Ferrari to Cars, throwing an exception if
they can''t be casted). You can cut it off, but bad things can happen if
you cast from, like saying, Cars to Ferraris.

/*!
* This is used to recast the whole list because elements can inherit
each other. Win32 only.
*/
template<class NewElementType>
List<NewElementType>&
Recast() {
// we should be aware if both ElementType and NewElementType are
compatible
ElementType 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 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


这篇关于施工问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆