问题描述
大家好,
设计模板类时遇到问题。我先描述一下我的场景
然后再展示我到目前为止所得到的:
需要一个类来提供指针/数组 - 喜欢进入一个物理区域
内存
位于一块定制硬件上 - 这个内存只能通过机器特定的i / $
所以我想在课堂上隐藏所有这些。我是
想象
该类将提供类似于向量或字符串类的行为 -
是,提供operator []访问,可能是使用++的指针式行为和 -
运算符和* dereference运算符。
类可能会被用作:
pmem< char>物理;
physmem [0] =''A''; //在偏移0处存储''A''
physmem ++;
physmem [0] =''B''; //在偏移1处存储''B''
char ch = * physmem;
甚至可能
cout<<物理;
即物理以某种方式衰变到一个char *指针..
我很难决定我应该提供什么样的设施
提供,
真的是由于缺乏C ++经验
我到目前为止的课程看起来像:
模板< class type>
class pmem
{
public:
pmem():index(0){}
类型& operator(size_t offset);
const type& operator(size_t offset)const;
private:
size_t index;
类型*查看;
//私人会员将物理内存映射/取消映射
//进入视图 ;缓冲区
}
类必须在视图中映射/取消映射物理内存 - 即非常相似
到分段内存架构,所以[]运营商需要处理这个
和
提供严格控制的线性。访问这个分段模型。
在物理内存中有一些内存区域可以很容易用正常的C结构表示 - 我想要避免的是代码
是这样的:
struct mystruct
{
int member1;
int member2;
};
mystruct ms;
physmem.read(& ms,sizeof( mystruct));
cout<< ms.member1<<结束;
并用类似的东西替换它:
pmem< mystruct *> physmem;
cout<< physmem-> member1;
希望我在这里提供了一个足够好的例子..
有没有人对这方面的最佳实践有任何建议类型
的东西,好的
书籍可读吗?我可以非常愉快地编写这个东西,它的设计阶段
这是目前阻碍我的b / b
。
TIA
詹姆斯
Hi all,
Having problems designing a template-class. I''ll describe my scenario
first then show what I''ve come up with so far:
Need a class to provide pointer/array-like access to an area of physical
memory
located on a piece of custom hardware - this memory is only accessible
using machine specific i/o so I want to hide all this in a class. I''m
imagining
that the class will provide behaviour similar to a vector or string class -
that
is, provide operator[] access, maybe pointer-like behaviour using ++ and --
operators and the * dereference operator.
The class might be used like:
pmem<char> physmem;
physmem[0] = ''A''; // store ''A'' at offset 0
physmem++;
physmem[0] = ''B''; // store ''B'' at offset 1
char ch = *physmem;
and maybe even
cout << physmem;
i.e. physmem somehow "decays" to a char* pointer..
I''m having alot of difficulty deciding what kind of facility I should
provide,
really due to lack of experience with C++
my so far class looks like:
template <class type>
class pmem
{
public:
pmem() : index(0) {}
type & operator(size_t offset);
const type &operator(size_t offset) const;
private:
size_t index;
type *view;
// private members to map/unmap the physical memory
// into the "view" buffer
}
The class must map/unmap the physical memory in views - i.e. very similar
to a segmented memory architecture, so the [] operators need to handle this
and
provide a strictly controlled "linear" access to this segmented model.
Within the physical memory there are memory regions which would be
easily represented by normal C structures - what I want to avoid is code
like this:
struct mystruct
{
int member1;
int member2;
};
mystruct ms;
physmem.read(&ms , sizeof(mystruct));
cout << ms.member1 << endl;
and replace it with something similar to:
pmem<mystruct *> physmem;
cout << physmem->member1;
Hope I''ve provided a good enough example here..
Does anyone have any suggestions as to best practises for this type of
thing, good
books to read? I can code the thing up quite happily, its the design stage
which is
hindering me at the moment.
TIA
James
推荐答案
花大量时间设计东西总是一件好事
因为一旦你明白了这个类的
规格,编码就可以很快完成(es)。
-
Karthik。
It is always a good thing to spend a lot of time in designing things
since coding can be wrapped up soon once you are clear about the
specifications of the class(es).
--
Karthik.
哎呀!我的意思是 -
模板< typename T> 。
-
Karthik。
Oops ! I meant -
template <typename T> .
--
Karthik.
Best
Kai-Uwe Bux
Best
Kai-Uwe Bux
这篇关于为内存访问设计模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!