由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 模板对象能不能作为成员变量使用
相关主题
C++ template problemvector::iterator不对
C++ linking 弱问 (one file)问一个C++的问题
一个关于C++ template和overload的问题请教一下这个template function在gcc下要怎么修改
boost serialization的问题template
Help: who has gcc 4.0 or higher这个是什么设计模式?
where to define my template function模板识别问题
C++ question about template typedefC++中如何引用模板类中的模板函数
template 类的继承问题模板类中的一个类型问题?
相关话题的讨论汇总
话题: templated话题: your话题: derived话题: 模板
进入Programming版参与讨论
1 (共1页)
f*******s
发帖数: 440
1
比如vector
需要用作成员变量
而且不能 instantiate
因为我的这个类要针对vector的各种T进行操作
有办法吗
谢谢
t****t
发帖数: 6806
2
no, must instatiate. however, you may use a templated derived class, and do
the real work with virtual function.

【在 f*******s 的大作中提到】
: 比如vector
: 需要用作成员变量
: 而且不能 instantiate
: 因为我的这个类要针对vector的各种T进行操作
: 有办法吗
: 谢谢

f*******s
发帖数: 440
3
我想用模板实现这样一个图像数据应用
图像数据存放在一个ImagingData类的一个连续内存区域
template
class ImagingData {
T* dataPtr ;
};
那应该怎样方便地使用这个类的对象呢?
比如我要在数据操作类里面把ImagingData类的对象作为成员变量
以便访问
那是不是就只能把操作类也做成模板类?
这样岂不是很不方便?
每个类都必须loadData之后重新实例化?
有没有办法对操作类写一些模板函数
在loadData之后,
能在类内部访问ImagingData对象
比如操作类存一个ImagingData的父类指针
然后每个模板函数内部cast后使用
但是模板函数好像又不能没有带模板的参数
有没有其他合适的方法啊?
谢谢了

do

【在 t****t 的大作中提到】
: no, must instatiate. however, you may use a templated derived class, and do
: the real work with virtual function.

t****t
发帖数: 6806
4
template is compile-time. you can't instantiate "after" executing something.
you must instantiate before you run; in other words, you have to
instantiate everything that you could possibly use.
with that in mind, reconsider your requirement.

【在 f*******s 的大作中提到】
: 我想用模板实现这样一个图像数据应用
: 图像数据存放在一个ImagingData类的一个连续内存区域
: template
: class ImagingData {
: T* dataPtr ;
: };
: 那应该怎样方便地使用这个类的对象呢?
: 比如我要在数据操作类里面把ImagingData类的对象作为成员变量
: 以便访问
: 那是不是就只能把操作类也做成模板类?

f*******s
发帖数: 440
5
谢谢你的回答。
你说的我能理解。
我的程序在instantiate部分
应该是写成switch的形式,
然后根据文件类型,
实例化不同的模板对象。
我的问题是在其他类里面
怎么使用这个模板对象呢?
具体来说,
怎么声明一个成员变量
要求是有T的?
问题可能有点低级
请见谅 呵呵

something.

【在 t****t 的大作中提到】
: template is compile-time. you can't instantiate "after" executing something.
: you must instantiate before you run; in other words, you have to
: instantiate everything that you could possibly use.
: with that in mind, reconsider your requirement.

t****t
发帖数: 6806
6
i have replied your question on the first place: you may use a templated
derived class. for example:
class base {
protected:
virtual void process() = 0;
};
template
class derived : public base {
vector data;
public:
void process() { ... }
};
/* in main() */
base* b;
switch (wanted_type) {
case INT:
b=new derived(...);
break;
case DOUBLE:
b=new derived(...);
break;
//...
}
b->process();

【在 f*******s 的大作中提到】
: 谢谢你的回答。
: 你说的我能理解。
: 我的程序在instantiate部分
: 应该是写成switch的形式,
: 然后根据文件类型,
: 实例化不同的模板对象。
: 我的问题是在其他类里面
: 怎么使用这个模板对象呢?
: 具体来说,
: 怎么声明一个成员变量

f*******s
发帖数: 440
7
恩 我的程序正式这样做的
我前面也说过了,
把这个base对象放在处理类
class imageDisplay{
Base * data;
}
但问题是怎么在imageDisplay或者在main()中使用这个类呢?
用那个虚process函数的话,没有参数或者返回值啊
一旦参数或者返回值带T
又会碰到同样的问题

【在 t****t 的大作中提到】
: i have replied your question on the first place: you may use a templated
: derived class. for example:
: class base {
: protected:
: virtual void process() = 0;
: };
: template
: class derived : public base {
: vector data;
: public:

t****t
发帖数: 6806
8
that why i asked you to reconsider your request. you have to define your
interface first. let's say you can have a templated parameter. then your
caller must be templated, isn't it?
define your interface first. just assume you have your function defined, and
try to "use" it in the main program. if you can write it with correct
syntax, you are fine. otherwise your interface is broken.

【在 f*******s 的大作中提到】
: 恩 我的程序正式这样做的
: 我前面也说过了,
: 把这个base对象放在处理类
: class imageDisplay{
: Base * data;
: }
: 但问题是怎么在imageDisplay或者在main()中使用这个类呢?
: 用那个虚process函数的话,没有参数或者返回值啊
: 一旦参数或者返回值带T
: 又会碰到同样的问题

1 (共1页)
进入Programming版参与讨论
相关主题
模板类中的一个类型问题?Help: who has gcc 4.0 or higher
一个C++ template的问题where to define my template function
tempalte as the overloaded conversion operatorC++ question about template typedef
sgi stl 源代码一问template 类的继承问题
C++ template problemvector::iterator不对
C++ linking 弱问 (one file)问一个C++的问题
一个关于C++ template和overload的问题请教一下这个template function在gcc下要怎么修改
boost serialization的问题template
相关话题的讨论汇总
话题: templated话题: your话题: derived话题: 模板