C++ 中引用和指针返回类型有什么区别?

发布于 2024-10-30 06:44:03 字数 576 浏览 1 评论 0原文

如果我要在 C++ 中创建一个简单的对象,返回成员的地址与返回指针之间有什么区别。据我所知,C++ 没有自动垃圾回收功能,因此它不会保留引用计数。那么为什么有人会这样做:

class CRectangle {
public:
    string& getName( );
    int&    getWidth( );
    int&    getHeight( );
private:
    string  name;
    int     height;
    int     width;
};

而不是这样:

class CRectangle {
public:
    string* getName( );
    int*    getWidth( );
    int*    getHeight( );
private:
    string  name;
    int     height;
    int     width;
};

我意识到这些将允许您访问成员数据,但我不关心这个简单示例中的正确封装。那么有什么区别呢?加速?可读性?风格?

If I were to create a simple object in C++, what is the difference between returning an address of the member vs. returning a pointer. As far as I'm aware, C++ doesn't have automatic garbage collection so it wouldn't be keeping a reference count. So why would someone do it this way:

class CRectangle {
public:
    string& getName( );
    int&    getWidth( );
    int&    getHeight( );
private:
    string  name;
    int     height;
    int     width;
};

rather than this way:

class CRectangle {
public:
    string* getName( );
    int*    getWidth( );
    int*    getHeight( );
private:
    string  name;
    int     height;
    int     width;
};

I realize these would allow you to access member data, but I'm not concerned about proper encapsulation in this simple example. So whats the difference? Speedup? Readability? Style?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

油焖大侠 2024-11-06 06:44:03

&(在此上下文中)并不意味着“地址”。

声明为 string& 的函数getName( ); 返回一个引用,而不是一个指针。引用本质上是另一个对象的别名。因此,在这种情况下,它不会返回对象名称的副本或指向该名称的指针,而是返回对名称本身的引用。因此,对返回对象的任何更改都会直接应用于对象的名称。

您可以通过返回指针来实现相同的目的,但有两个显着差异:

  • 指针需要特殊语法才能访问(*-> 运算符),而引用的使用方式与使用对象本身的方式完全相同。
  • 指针可以为 null,而引用则不能。因此,每当使用指针时,您都会向代码读者发出“此值可能为 null”的信号

The & (in this context) doesn't mean "address of".

A function declared as string& getName( ); returns a reference, not a pointer. A reference is essentially an alias for another object. So in this case, it doesn't return a copy of the object's name, or a pointer to the name, but a reference to the name itself. So any changes to the returned object are directly applied to the object's name.

You could achieve the same by returning a pointer, but there'd be two significant differences:

  • a pointer requires special syntax to access (the * and -> operators), whereas a reference is used the exact same way you'd use the object itself.
  • a pointer can be null, a reference cannot. So any time a pointer is used, you are signalling to the reader of the code that "this value may be null"
雨的味道风的声音 2024-11-06 06:44:03

在这种情况下,& 表示引用,而不是地址。

  • 在格式良好的程序中,指针可以为 0 - 引用不能为 0。
  • 当您返回指针时,所有权可能会更加不清楚。
  • 可读性是主观的——我建议尽可能使用参考。
  • 就效率而言,没有真正的区别。

In this context, the & means reference - not address-of.

  • A pointer can be 0 - A reference cannot, in a well-formed program.
  • Ownership may be more unclear when you return a pointer.
  • Readability is subjective - I'd say use a reference when you can.
  • For efficiency, there's no real difference.
一百个冬季 2024-11-06 06:44:03

首先,&形式在 C++ 中称为“引用”。 * 形式称为“指针”或
“地址”。

加速 - 没有区别。可读性 - 对于引用来说可能有一点优势,在上栈代码中没有解除引用。

风格 - 显式破坏封装的风格足以掩盖其他任何内容。如果您想要一个包含裸数据的对象,那么全公共结构也可以。

First off, the & form is called a "reference" in C++. The * form is called "pointer" or
"address of".

Speedup - no difference. Readability - probably a slight advantage for references, no derefencing in the upstack code.

Style - explicit breaking of encapsulation is bad enough style to obbscure anything else. If you want an object with naked data, an all-public struct would do just as well.

沒落の蓅哖 2024-11-06 06:44:03

我认为从属性类型方法(例如 getWidth())返回指向整数的指针没有任何好处。它增加了必须跟踪内存所有权的负担;但实际上可能会增加返回类型的带宽(int 和指针大小都是特定于平台的,并且可以并且确实会有所不同,但典型大小分别为 32 位和 64 位)。

There's no benefit I can think of in returning a pointer to an integer from a property-type method such as getWidth(). It adds the burden of having to track memory ownership; but is actually likely to increase the bandwidth of the return type (both int and pointer sizes are platform specific, and can and do vary, but typical sizes are 32 and 64 bits respectively).

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