p.上缺少成员初始化程序列表的示例. 184《使用C ++进行编程的原理和实践》,第二版 [英] Missing example of a Member Initializer List on p. 184 of Programming Principles and Practice Using C++, 2nd ed

查看:100
本文介绍了p.上缺少成员初始化程序列表的示例. 184《使用C ++进行编程的原理和实践》,第二版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在编程:使用C ++的原理和实践"(第2版,第3版)的第6章中遇到麻烦.

I'm currently having trouble with part of Chapter 6 of "Programming: Principles and Practice Using C++" (2nd ed, 3rd printing).

根据该书的索引,在第184页上有一个成员初始化列表的示例.

According to the book's index, an example of a member initializer list is on page 184.

第184页的内容如下:

Part of page 184 reads as follows:

在这里,我们将只提供两个成员函数,以便为我们提供一种更方便的初始化令牌的方法:

"Here, we'll just provide two member functions to give us a more convenient way of initializing Tokens:

 class Token {
    public: 
    char kind; // what kind of token
    double value; // for numbers: a value
    };

我们现在可以初始化(构造")令牌对象.例如:

We can now initialize ("construct") Token objects. For example:

Token t1 {'+'}; // initialize t1 so that t1.kind = '+'
Token t2 {'8,' 11.5}; // initialize t2 so that t2.kind = '8' and t2.value = 11.5

"

这些代码集之一是索引指示的成员初始化程序列表的示例吗?我有些困惑,因为基于另一个Stackoverflow答案(构造函数中这种奇怪的冒号-((::))语法是什么?),我以为成员初始化器列表看起来更像是:

Is one of these sets of code an example of a member initializer list as the index would indicate? I'm somewhat confused because, based on another Stackoverflow answer (What is this weird colon-member (" : ") syntax in the constructor? ), I thought a member initializer list would look something more like:

Name_value(string n, int v)
                :name(n), value(v) { }

(由Chrinkus在Github上的以下代码中找到: https://github.com/Chrinkus/stroustrup-ppp/blob/master/chapter06/ex04_Name_value.cpp )

(found within the following code by Chrinkus on Github: https://github.com/Chrinkus/stroustrup-ppp/blob/master/chapter06/ex04_Name_value.cpp )

class Name_value {
    public:
        string name;
        int value;
        Name_value(string n, int v)
            :name(n), value(v) { }
};

我认为我误解的可能性比书中有错别字的可能性大,但尽管如此,我还是觉得这部分令人困惑.感谢您提供的任何反馈意见.

I think it's far more likely that I'm mistaken than that there is a typo on the book, but nonetheless, I'm finding this part of the book confusing. I appreciate any feedback you may be able to offer.

更新:我发现第6章的演练包含以下代码很有趣:

Update: I find it interesting that the drill for chapter 6 includes this code:

Token(char ch) // make a Token from a char
        : kind(ch), value(0)
    {}
    Token(char ch, double val) // make a Token from a char and a double
        : kind(ch), value(val)
    {}

那些是成员初始值设定项,不是吗?我想知道作者在写这儿,我们将只提供两个成员函数,以便为我们提供一种更方便的初始化令牌的方法"时是否要引用这两个函数的方法.

Those are member initializers, are they not? I wonder if the author meant to refer to those two functions when he wrote "Here, we'll just provide two member functions to give us a more convenient way of initializing Tokens:"

推荐答案

我通过电子邮件与Bjarne取得联系,他立即通知我这是一个错误,该错误将在下一次印刷中修复.因此,我的猜测是第184页的内容或多或少如下:

I reached out to Bjarne by email, and he promptly informed me that this was a bug that would be fixed in the next printing. So my guess is that that section of page 184 was meant to read more or less as follows:

在这里,我们将只提供两个成员函数,以便为我们提供一种更方便的初始化令牌的方法:

"Here, we'll just provide two member functions to give us a more convenient way of initializing Tokens:

 class Token {
    public: 
    char kind; // what kind of token
    double value; // for numbers: a value
    Token(char ch)  // make a Token from a char
        :kind(ch), value(0) { }    
    Token(char ch, double val) // make a Token from a char and a double
        :kind(ch), value(val) { }

};

我们现在可以初始化(构造")令牌对象. . ."

We can now initialize ("construct") Token objects . . ."

上面代码中的两个成员初始化器列表取自第6章演练的示例代码,可在此处找到: http://www.stroustrup.com/Programming/calculator02buggy.cpp (请注意,此代码包含一些读者打算解决的故意错误在演练中.)

The two member initializer lists in the above code were taken from the sample code for the chapter 6 drill, which is available here: http://www.stroustrup.com/Programming/calculator02buggy.cpp (Please note that this code contains some intentional bugs that the reader is meant to solve within the drill exercise.)

我希望这对那些对成员初始化程序列表感到困惑的人有用.到目前为止,这本书对我来说非常有用,我打算继续阅读.

I hope this will be useful to anyone else who was confused about member initializer lists. The book has been very useful for me so far and I plan to continue to read through it.

这篇关于p.上缺少成员初始化程序列表的示例. 184《使用C ++进行编程的原理和实践》,第二版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆