当我需要通过指针传递(而不是通过引用)时

发布于 2024-09-28 05:04:46 字数 208 浏览 7 评论 0原文

当我无法通过引用传递参数并且需要使用指针时,您能给我一个例子吗?我找到了一个例子,但我不确定。

假设您有一个从基类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

卖梦商人 2024-10-05 05:04:46

唯一一次不能使用引用而必须使用指针的情况是,如果您通过传递空指针来允许“无参数”的概念。

但是,当您实际存储指向所传递的内容的指针时,您可能希望使用指针作为参数。大多数 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.

荒路情人 2024-10-05 05:04:46

如果有编码指南(如 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.,

赴月观长安 2024-10-05 05:04:46

另一种情况:如果您传递的内容是 varargs 之前的最后一个参数:

void fn1(A &a, ...); // Uh oh
void fn2(A *a, ...); // Good

我不知道这是否是标准所要求的,或者只是我使用的 C++ 编译器的实现中的一个错误。

Another case: if the thing you're passing is the last argument before varargs:

void fn1(A &a, ...); // Uh oh
void fn2(A *a, ...); // Good

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.

浪荡不羁 2024-10-05 05:04:46

通常,您将指针用于以下两件事之一:

  • 可重新分配性 - 您不能重新绑定
    参考。
  • 空指针 - 没有这样的指针
    事物作为空引用。

如果您的预期用例不需要这两个属性中的任何一个,请使用引用。否则,使用指针。

Typically, you use pointers for one of two things:

  • Reassignability - you can't rebind a
    reference.
  • Null pointers - there's no such
    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.

萌化 2024-10-05 05:04:46

如果要允许缺少对象,则需要使用指针:

// This allows DoSomething to receive pointers to NULL, which cannot
// be done with references
void DoSomething(Something *pSomething)
{
  if (pSomething)
  {
    ...
  }
}

int main()
{
  Something *pSomething=NULL;

  DoSomething(pSomething);
}

If you want to allow the lack of an object, you need to use pointers:

// This allows DoSomething to receive pointers to NULL, which cannot
// be done with references
void DoSomething(Something *pSomething)
{
  if (pSomething)
  {
    ...
  }
}

int main()
{
  Something *pSomething=NULL;

  DoSomething(pSomething);
}
乖乖公主 2024-10-05 05:04:46

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.

枯寂 2024-10-05 05:04:46

唯一的原因是您是否需要传递 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'

楠木可依 2024-10-05 05:04:46

我认为如果你想传递一个函数,你必须通过指针传递它。我不明白如何通过引用传递该函数。

例如,采用以下函数:

#include <iostream>
#include "math.h"

void myfun (double value, size_t nofloops, double (*function)(double))
   {
   std::cout << value << std::endl;
   for (size_t i=0;i<nofloops;++i)
      {
      value = function(value);
      std::cout << value << std::endl;
      }
   std::cout << "------------------" << std::endl;
   }

void main()
   {
   myfun(100,10,sin);
   myfun(100,10,cos);
   myfun(100,10,sqrt);
   }

这个小实用程序中的函数多次执行给定函数,并将结果作为下一次迭代的输入。我不明白如何通过引用传递该函数。

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:

#include <iostream>
#include "math.h"

void myfun (double value, size_t nofloops, double (*function)(double))
   {
   std::cout << value << std::endl;
   for (size_t i=0;i<nofloops;++i)
      {
      value = function(value);
      std::cout << value << std::endl;
      }
   std::cout << "------------------" << std::endl;
   }

void main()
   {
   myfun(100,10,sin);
   myfun(100,10,cos);
   myfun(100,10,sqrt);
   }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文