当我需要通过指针传递(而不是通过引用)时
当我无法通过引用传递参数并且需要使用指针时,您能给我一个例子吗?我找到了一个例子,但我不确定。
假设您有一个从基类 B
派生的类 D
。如果你想这样做,你需要指针:
void function(B* b){...}
int main{
D* d;
function(d);
}
Can you give me an example when I can't pass argument by reference and I need to use pointer. I've found an example, but I'm not sure.
Suppose you have a class D
derived from the base class B
. You need pointer if you want do so:
void function(B* b){...}
int main{
D* d;
function(d);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
唯一一次不能使用引用而必须使用指针的情况是,如果您通过传递空指针来允许“无参数”的概念。
但是,当您实际存储指向所传递的内容的指针时,您可能希望使用指针作为参数。大多数 C++ 开发人员会注意到您没有使用参考,并特别注意文档的内容。
The single time where you can not use a reference and must use a pointer is if you allow the concept of "no argument" by passing a null pointer.
However, you might want to use pointers as arguments when you are actually storing a pointer to whatever was passed. Most C++ developpers will notice that you aren't using a reference and pay special attention to what your documentation says.
如果有编码指南(如 Google 的)要求使用指针参数,那么您就应该这样做。
时,才使用指针形式参数声明自己的函数
实际参数已经是最自然的指针,或者
你要将该指针存储在某个地方。
可能有更多的情况,但我认为你明白了:当你有选择时(没有编码指南另有说明),更喜欢参考文献。
干杯&呵呵,,
If there is a coding guideline (like Google's) that says to use pointer arguments, then that's what you do.
Otherwise, only declare your own function with pointer formal argument when
a nullpointer is a valid & meaningful actual argument, or
the actual argument is most naturally pointer already, or
you're going to store that pointer somewhere.
Possibly more cases, but I think you get the drift: when you have a choice (no coding guideline saying otherwise), prefer references.
Cheers & hth.,
另一种情况:如果您传递的内容是 varargs 之前的最后一个参数:
我不知道这是否是标准所要求的,或者只是我使用的 C++ 编译器的实现中的一个错误。
Another case: if the thing you're passing is the last argument before varargs:
I don't know if this is required by the standard, or is just a bug in the implementation of the C++ compiler I use.
通常,您将指针用于以下两件事之一:
参考。
事物作为空引用。
如果您的预期用例不需要这两个属性中的任何一个,请使用引用。否则,使用指针。
Typically, you use pointers for one of two things:
reference.
thing as a null reference.
If your intended use case does not need either of those two properties, use a reference. Else, use a pointer.
如果要允许缺少对象,则需要使用指针:
If you want to allow the lack of an object, you need to use pointers:
http://www.daniweb.com/forums/thread216353.html
单链表示例指针和指针的指针用作函数参数。
http://www.daniweb.com/forums/thread216353.html
Singly linked lists example were pointers and pointer of pointers are used as function parameters.
唯一的原因是您是否需要传递 null。即你想调用该函数说“我没有其中之一”
the only reason is if you need to pass null. I.e you want to call the function saying 'I haven't got one of those'
我认为如果你想传递一个函数,你必须通过指针传递它。我不明白如何通过引用传递该函数。
例如,采用以下函数:
这个小实用程序中的函数多次执行给定函数,并将结果作为下一次迭代的输入。我不明白如何通过引用传递该函数。
I think that if you want to pass a function, you have to pass it by pointer. I don't see how you can pass the function by reference.
For example, take the following function:
The function in this small utility executes the given function a number of times, taking the result as input in the next iteration. I can't see how you can pass the function by reference.