是否使用指针。 [英] To use a pointer or not.

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

问题描述

我为一个小矢量

数学库创建了一个简单的三维矢量类。班级成员是三个'x'','y''和''z''。

想法是将多边形顶点表示为指向3d的指针数组

向量。我的问题是,我应该制作

vecctor指针的x,y和z成员吗?什么时候使用

指针或只是普通变量的一般指导原则?


-

Hans

I''ve created a simple three-dimensional vector class for a small vector
math library. The class members are three doubles ''x'', ''y'' and ''z''.
The idea is to represent polygon vertices as an array of pointers to 3d
vectors. My question is, should I make the x, y and z members of the
vecctor pointers? What are the general guidelines of when to use
pointers or just plain variables?

--
Hans

推荐答案

我的问题是,我应该制作
vecctor指针的x,y和z成员吗?
My question is, should I make the x, y and z members of the
vecctor pointers?




你的意思是什么?矢量指针的成员?指针是否有一个

的成员?


ben



What do you mean "members of the vector pointers"? Does a pointer have a
member??

ben


哎呀,对不起!我一定是在睡觉。


保留x,y和z私人会员并用适当的会员包裹起来

功能。这种方式即使你后来决定将它们改为指针,你也不会破坏用户代码(所有封装都是关于)。


ben
Oops, sorry! I must be sleeping.

Keep x, y and z private members and wrap them up with proper member
functions. This way even if you later decide to change them to pointers, you
won''t break user code (all encapsulation is about).

ben
你的意思是什么?矢量指针的成员?指针是否有成员?

What do you mean "members of the vector pointers"? Does a pointer have a
member??

ben



a0 ****** @ NOstudent.SPAMhis.se 写道:
我创造了一个简单的三维矢量类的小矢量数学库。类成员是三个x,y和z。
这个想法是将多边形顶点表示为指向3d
向量的指针数组。
我的问题是,我应该制作
vecctor指针的x,y和z成员吗?


No.

何时使用
指针或只是普通变量的一般指导原则是什么?
I''ve created a simple three-dimensional vector class for a small vector
math library. The class members are three doubles ''x'', ''y'' and ''z''.
The idea is to represent polygon vertices as an array of pointers to 3d
vectors. My question is, should I make the x, y and z members of the
vecctor pointers?
No.
What are the general guidelines of when to use
pointers or just plain variables?




常识。要在矢量中直接保留三个双打,你需要
需要3 * sizeof(双)字节。为了让它们成为你向量中的指针

你需要至少3 *(sizeof(double)+ sizeof(double *))加上你将要b / b
必须执行间接,你必须确保指针是

有效等等。通过制作指针你获得了什么?

的大小向量对象本身较少(3 *(sizeof(double)-sizeof(double *))。

这是一个聪明的举动吗?我不喜欢我不这么认为。


你可以做得更好:把它们作为一个指向三个双打的指针

(数组)并提供某种形式快速和精益分配机制对于b $ b避免一直通过通用的''new []''和''删除[]''。

向量的大小下降了多少

(3 * sizeof(double) - sizeof(double *)),可以看作是重要的。

你仍然需要执行间接,分配和释放,

和验证,复制并不像直接双打那样微不足道,

等等。


V



Common sense. To keep three doubles directly in your vector you will
need 3*sizeof(double) bytes. To keep them as pointers in your vector
you will need at least 3*(sizeof(double) + sizeof(double*)) plus you will
have to perform indirection, you will have to make sure the pointers are
valid and so on. What do you gain by making them pointers? The size of
the vector object itself is less by (3*(sizeof(double)-sizeof(double*)).
Is that a smart move? I don''t think so.

You could do a bit better: keep them as a single pointer to three doubles
(an array) and provide some kind of fast and lean allocation mechanism to
avoid going through the generic ''new[]'' and ''delete[]'' all the time.
That takes the size of your vector down by as much as
(3*sizeof(double) - sizeof(double*)), which can be viewed as significant.
You still need to perform indirection, and allocation, and deallocation,
and validation, and copying is not as trivial as with straight doubles,
and so on.

V


这篇关于是否使用指针。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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