构造函数初始化列表:来自 C++ 的代码入门知识,第 16 章

发布于 2024-09-01 18:38:06 字数 1083 浏览 11 评论 0原文

在《C++ Primer》第 16 章末尾,我遇到了以下代码(我删除了一堆行):

class Sales_item {
public:
    // default constructor: unbound handle
    Sales_item(): h() { }
private:
    Handle<Item_base> h;   // use-counted handle
};

我的问题出在 Sales_item(): h() { } 行。

为了完整起见,让我也引用我认为与我的问题相关的 Handle 类模板的部分(我认为我不需要显示 Item_base 类):

template <class T> class Handle {
public:
    // unbound handle
    Handle(T *p = 0): ptr(p), use(new size_t(1)) { }
private:
    T* ptr;          // shared object
    size_t *use;     // count of how many Handles point to *ptr
};

我本来期望类似的东西:

a) Sales_item(): h(0) { } 这是作者在前面的章节中反复使用的约定,或者

b) Handle()(如果意图是)调用 Handle 类的默认构造函数。

相反,这本书有 Sales_item(): h() { }。我的直觉反应是这是一个拼写错误,因为 h() 看起来与函数声明非常相似。另一方面,我只是尝试在 g++ 下编译并运行使用此类的示例代码,它似乎工作正常。有什么想法吗?

编辑:所有好的答案,谢谢!在这中间的 30 分钟内,我找到了同一本书第 12 章中的相关引用:“当我们初始化类类型的成员时,我们指定要传递给该成员类型的构造函数之一的参数。我们可以使用该类型的任何构造函数。”正如你们都指出的,在这种情况下我们传递零参数。

Toward the end of Chapter 16 of the "C++ Primer" I encountered the following code (I've removed a bunch of lines):

class Sales_item {
public:
    // default constructor: unbound handle
    Sales_item(): h() { }
private:
    Handle<Item_base> h;   // use-counted handle
};

My problem is with the Sales_item(): h() { } line.

For the sake of completeness, let me also quote the parts of the Handle class template that I think are relevant to my question (I think I don't need to show the Item_base class):

template <class T> class Handle {
public:
    // unbound handle
    Handle(T *p = 0): ptr(p), use(new size_t(1)) { }
private:
    T* ptr;          // shared object
    size_t *use;     // count of how many Handles point to *ptr
};

I would have expected something like either:

a) Sales_item(): h(0) { } which is a convention the authors have used repeatedly in earlier chapters, or

b) Handle<Item_base>() if the intention was to invoke the default constructor of the Handle class.

Instead, what the book has is Sales_item(): h() { }. My gut reaction is that this is a typo, since h() looks suspiciously similar to a function declaration. On the other hand, I just tried compiling under g++ and running the example code that uses this class and it seems to be working correctly. Any thoughts?

EDIT: All good answers, thanks! I have in the intervening 30 minutes tracked down a relevant quote from chapter 12 of the same book: "When we initialize a member of class type, we are specifying arguments to be passed to one of the constructors of that member's type. We can use any of that type's constructors." And as you all pointed out, in this case we are passing zero arguments.

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

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

发布评论

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

评论(3

蓦然回首 2024-09-08 18:38:06

Sales_item(): h() { } 所拥有的是一个带有数据成员初始化的构造函数。

我本来期望类似的东西:

a) Sales_item(): h(0) { } 这是作者在前面的章节中反复使用的约定,或者

这不是必需的,因为Handle() 可以在没有参数的情况下调用。 (它的一个参数有一个默认值,因此可以省略。)

b) Handle()(如果目的是调用 Handle 类的默认构造函数)。

这根本就是不正确的语法。该语法用于基类,这很好,因为任何类只能从 Handle() 继承一次。然而,它可以有许多该类型的数据元素,因此为了初始化正确的数据成员,使用其名称而不是其类型。


顺便说一句,这是一本非常好的书,你可以从中学习。完成后,您可能需要查看权威 C++ 指南和列表以获取更多好的信息。

What you have with Sales_item(): h() { } is a constructor with a data member initialization.

I would have expected something like either:

a) Sales_item(): h(0) { } which is a convention the authors have used repeatedly in earlier chapters, or

This isn't necessary because the constructor of Handle<Item_base>() can be invoked without an argument. (Its one argument has a default value, so it can be omitted.)

b) Handle<Item_base>() if the intention was to invoke the default constructor of the Handle class.

This is simply incorrect syntax. That syntax is used for base classes and there it is fine, since any class can inherit only once from Handle<Item_base>(). However, it can have many data elements of that type, so in order to initialize the right data member, its name is used rather than its type.


That's a very good book you're learning from, BTW. Once you're through it, you might want to look at The Definitive C++ Guide and List for more good input.

带刺的爱情 2024-09-08 18:38:06

代码完全正确 - h() 表示使用默认构造函数来构造 h。在这种情况下,它也是不必要的,因为如果您不提供显式初始化,则将使用默认构造函数。默认构造函数是任何可以在没有参数的情况下调用的构造函数,句柄类具有默认构造函数,因为它具有默认的参数值。

The code is completely correct - the h() means use the default constructor to construct h. In this case it is also unecessary, as the default constructor will be used if you don't supply an explicit initialisation. A default constructor is any constructor that can be called without arguments, which the handle class has, due to it's defaulted argument value.

江心雾 2024-09-08 18:38:06

hSales_item 的一个成员变量的名称,它的类 Handle 有一个带默认参数的构造函数,所以 h( ) 正确构造成员。它相当于h(0)

h is the name of a member variable of Sales_item, and its class Handle has a constructor with a default parameter, so h() constructs the member properly. It is equivalent to h(0).

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