模板问题 [英] Template question

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

问题描述

大家好,


我有一个问题由以下代码说明:


template< class T>

A级{

T my_value;

};


在列表中,我'我希望存储指向A类物体的指针。但是,如果这些将是A< intor A< doubleor what(所有

),我不会在正手上知道
这些A的味道必须存储在同一个列表中)。然后,如果我从列表中检索对象,我可能必须知道

对象的味道,以便调用正确的例程来处理它们。 />

我的问题:C ++中的列表是什么样的?我迷路了,所以如果有人

有一些建议....


谢谢,


Jeroen

Hi all,

I have a question which is illustrated by the following piece of code:

template <class T>
class A {
T my_value;
};

In a list, I''d like to store pointers to objects of class A. But I don''t
know at forehand if these will be A<intor A<doubleor whatever (all
these flavours of A must be stored in the same list). And, lateron if I
retrieve the objects from the list I may have to know what flavour the
object was in order to call the proper routines for processing them.

My question: what does the list look like in C++? I''m lost, so if anyone
has some advice....

Thanks,

Jeroen

推荐答案

Jeroen写道:
Jeroen wrote:

大家好,


我有一个问题由以下代码说明:


模板< class T>

A类{

T my_value;

};


在列表中,我想存储指向A类对象的指针。但是,如果这些将是A< intor A< doubleor what(所有

这些A的味道必须存储在同一个列表中),我不会直接知道
。然后,如果我从列表中检索对象,我可能必须知道

对象的味道,以便调用正确的例程来处理它们。 />

我的问题:C ++中的列表是什么样的?我迷路了,所以如果有人

有一些建议....


谢谢,


Jeroen
Hi all,

I have a question which is illustrated by the following piece of code:

template <class T>
class A {
T my_value;
};

In a list, I''d like to store pointers to objects of class A. But I don''t
know at forehand if these will be A<intor A<doubleor whatever (all
these flavours of A must be stored in the same list). And, lateron if I
retrieve the objects from the list I may have to know what flavour the
object was in order to call the proper routines for processing them.

My question: what does the list look like in C++? I''m lost, so if anyone
has some advice....

Thanks,

Jeroen



您需要将所有不同类型存储在同一个列表中吗?后来你需要恢复列表中存储的对象类型吗?


抱歉,但是在C ++中无法完成。


john

You need to store all different types in the same list? And later you
need to recover what was the type of object stored in the list?

Sorry but in C++ that can''t be done.

john


Jeroen写道:
Jeroen wrote:

我有一个问题以下代码说明:


模板< class T>

A类{

T my_value;

};


在列表中,
I have a question which is illustrated by the following piece of code:

template <class T>
class A {
T my_value;
};

In a list,



你的定义是什么? ''list''?

What''s the definition of your ''list''?


我想存储指向A类对象的指针。
I''d like to store pointers to objects of class A.



没有A类直到你定义模板参数是什么。

只有类模板A。

There is no "class A" until you define what the template argument is.
There is only "class template A".


但是我

在正手上不知道这些是A< intor A< doubleor

无论如何(所有这些A的味道必须存储在同一个列表中)。
But I
don''t know at forehand if these will be A<intor A<doubleor
whatever (all these flavours of A must be stored in the same list).



没有风味这样的东西在C ++中。在这里你谈谈什么是

被称为实例化"相同的模板。它们是_different_

*类型*。不同类型的对象不能存储在相同的

容器中(除非您以某种非常棘手的方式定义容器并且特殊方式为
)。那么,''列表'的定义是什么?

There is no such thing as "flavour" in C++. Here you talk about what
is known as "instantiations" of the same template. They are _different_
*types*. Objects of different types cannot be stored in the same
container (unless you define your container in some very tricky and
special way). So, what''s the definition of your ''list''?


然后,如果我从列表中检索对象,我可能需要

知道对象的味道,以便调用正确的例程

来处理它们。

我的问题:C ++中的列表是什么样的?我迷路了,所以如果

,任何人都有一些建议....
And, lateron if I retrieve the objects from the list I may have to
know what flavour the object was in order to call the proper routines
for processing them.
My question: what does the list look like in C++? I''m lost, so if
anyone has some advice....



你想解决什么问题?您很可能需要一个异构容器。
需要一个异构容器。仔细看看。但是,如果给出这样模糊的问题定义,建议任何事情都是不可能的。


另请阅读常见问题解答。你在这里描述的内容通常是通过多态性来解决的(并不是那么困难)。但是你确实没有说过什么样的处理。你要做的,你的

类模板''''也没有任何功能,这就引出了问题,

为什么你不只是存储' ''''和'''''''''''''''''''$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

在通过电子邮件回复时删除资本''A'

我没有回复最热门的回复,请不要问`

What problem are you trying to solve? It is very likely that you
need a "heterogeneous container". Look it up. But it''s impossible
to advise anything given such vague "problem definition".

Also read the FAQ. What you seem to describe here is usually solved
through polymorphism (and it''s not all that difficult). But you did
not say what kind of "processing" you are going to do, nor does your
class template ''A'' have any functionality, which begs the question,
why don''t you just store ''ints'' and ''doubles'' in your ''list''?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask`

John Harrison写道:
John Harrison wrote:

Jeroen写道:
Jeroen wrote:

>大家好,

我有一个问题由以下
代码说明:模板< class T>
A类{
T my_value;
};

在列表中,我想存储指向A类对象的指针。但是我不知道这些是否是A< intor A< doubleor <无论如何(所有这些A的味道必须存储在同一个列表中)。
然后,如果我从列表中检索对象我可能必须知道对象的味道,以便调用正确的例程来处理它们。我的问题:这个列表在C ++中是什么样的?我迷路了,所以如果有人有一些建议....

谢谢,

Jeroen
>Hi all,

I have a question which is illustrated by the following piece of
code: template <class T>
class A {
T my_value;
};

In a list, I''d like to store pointers to objects of class A. But I
don''t know at forehand if these will be A<intor A<doubleor
whatever (all these flavours of A must be stored in the same list).
And, lateron if I retrieve the objects from the list I may have to
know what flavour the object was in order to call the proper
routines for processing them. My question: what does the list look like
in C++? I''m lost, so if
anyone has some advice....

Thanks,

Jeroen


您需要将所有不同类型存储在同一个列表中吗?后来你需要恢复列表中存储的对象类型吗?


很抱歉,但是在C ++中无法完成。


You need to store all different types in the same list? And later you
need to recover what was the type of object stored in the list?

Sorry but in C++ that can''t be done.



哦,拜托,约翰。没有足够的信息可以得出结论

无法完成用C ++。


V

-

请在通过电子邮件回复时删除资金''A' br />
我没有回复最热门的回复,请不要问

Oh, come on, John. There is not enough information to conclude that
it "can''t be done" in C++.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


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

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