问题描述
我有一个函数,我将一个迭代器传递给一个char *缓冲区(也是一个char *)。该函数需要递增迭代器。无论如何,我发现一个好的方法传递迭代器到函数是通过传递指针通过引用:
I have a function where I'm passing in an iterator to a char * buffer (which is also a char *). The function needs to increment the iterator. Anyway, I found that a good method of passsing the iterator into the function is by passing the pointer by reference:
bool myFunction(unsigned char * &iter)
{
...
我听说这是坏的形式,可能会导致问题。这里是我的同事建议我使用的方法:
However, I've heard that this is bad form and could cause problems. Here is the method that my coworker suggested I use:
typedef unsigned char * unsignedcharptr;
bool myFunction(unsignedcharptr &iter)
{
...
它看起来像我,他们都在做同样的事情。有这两种方法之间的编译器差异(我使用Visual Studio 2005)?哪个是正确的?
It looks to me like they're both doing the same thing. Is there a compiler difference between these two methods (I'm using Visual Studio 2005)? Which is correct?
推荐答案
我认为两者之间没有什么区别。你应该问你的同事为什么他相信有。
I don't think there's any difference between the two. You should ask your coworker why he believes there is.
可能是可维护性的原因,如果您想从 unsigned char *
到 char *
,您只需要更改一个地方。但是,我认为,这可能会导致其他问题。
What might be the cause is for maintainability, where if you wanted to switch from unsigned char *
to char *
you'd only have to change one place. But this, I think, could lead to other problems.
这篇关于在c ++中通过引用传递指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!