码迷,mamicode.com
首页 > 编程语言 > 详细

<<C++ 沉思录>> 中文人民邮电出版 勘误

时间:2015-05-22 17:14:56      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:c++

<<C++ 沉思录>> 中文人民邮电出版 勘误


这本中文版里面有各种坑爹的小错误. 比方说变量名的大小写, 同一个变量, 出现了大小写不一致, 等等问题都有.


然后今天感觉遇到了个语法问题. 关于继承权限的问题.


书中第八章的demo里面, 关于class Expr_node.

使用了protected关键字. 但是这里Expr_node是基类, 继承就会出现问题.


具体的代码如下:

class Expr_node
{
    friend ostream& operator << (ostream&, const Expr_node&);
    friend class Expr;

    int use;// @use is a counter to avoid copying objects.

    //protected:
    public:
        Expr_node(): use(1) { }
        virtual void print(ostream&) const = 0;
        virtual ~Expr_node() { }
        virtual int eval() const = 0;
};

这里用protected就会error.

protected本来就是为了明确不发生继承的区域. 这里这堆虚函数是要发生继承的.就是为了占坑了让子类去实现.

这里应该使用public, 而不是 protected.


由于可能在"挑战权威" , 所以把问题抛出来, 希望有心人能一起讨论.


下面是完整的代码. 可供测试


/*
    Programmer  :   EOF
    Date        :   2015.05.19
    File        :   8.4.cpp
    E-mail      :   jasonleaster@gmail.com

 */
#include <iostream>
#include <string>

using namespace std;


/*
   This @Expr_node is the base-class.
 */
class Expr_node
{
    friend ostream& operator << (ostream&, const Expr_node&);
    friend class Expr;

    int use;// @use is a counter to avoid copying objects.

    protected:
    //public:
        Expr_node(): use(1) { }
        virtual void print(ostream&) const = 0;
        virtual ~Expr_node() { }
        virtual int eval() const = 0;
};


class Expr
{
    friend ostream& operator<<(ostream&, const Expr&);
    Expr_node* p;

    public:
        Expr():p(NULL){}
        Expr(int);
        Expr(const string&, Expr);
        Expr(const string&, Expr, Expr);
        Expr(const Expr& t) { p = t.p; ++p->use; };

        Expr& operator=(const Expr&);

        ~Expr() { if(--p->use == 0) delete p;}

        int eval() const {return p->eval();}
};

ostream&
operator<<(ostream& o, const Expr_node& e)
{
    e.print(o);
    return o;
}

Expr&
Expr::operator=(const Expr& rhs)
{
    rhs.p->use++;
    if(--p->use == 0)
    {
        delete p;
    }

    p = rhs.p;
    return *this;
}

ostream&
operator<<(ostream& o, const Expr& t)
{
    t.p->print(o);
    return o;
}

class Int_node: public Expr_node
{
    friend class Expr;

    int n;

    Int_node(int k): n(k) { }
    void print(ostream& o) const { o << n;}
    int eval() const { return n;}
};

class Unary_node: public Expr_node
{
    friend class Expr;
    string op;
    Expr opnd;
    Unary_node(const string& a, Expr b):
        op(a), opnd(b) { }

    void print(ostream& o) const
    {
        o << "(" << op << opnd << ")";
    }

    int eval() const
    {
        if(op == "-")
        {
            return -opnd.eval();
        }

        throw "error, bad op" + op + "int UnaryNode";
    }
};

class Binary_node: public Expr_node
{
    friend class Expr;
    string op;

    Expr left;
    Expr right;

    Binary_node(const string& a, Expr b, Expr c):
        op(a), left(b), right(c) { }

    void print(ostream& o) const
    {
        o << "(" << left << op << right << ")";
    }

    int eval() const
    {
        int op1 = left.eval();
        int op2 = right.eval();

        if(op == "-") return op1 - op2;
        if(op == "+") return op1 + op2;
        if(op == "*") return op1 * op2;
        if(op == "/") return op1 / op2;

        if(op == "/" && op2 != 0) return op1/ op2;

        throw "error, bad op" + op + "int BinaryNode";
    }
};

Expr::Expr(int n)
{
    p = new Int_node(n);
}

Expr::Expr(const string& op, Expr t)
{
    p = new Unary_node(op, t);
}

Expr::Expr(const string& op, Expr left, Expr right)
{
    p = new Binary_node(op, left, right);
}


int main()
{
    Expr t = Expr("*", Expr("-", 5), Expr("+", 3, 4));
    cout << t << " = " << t.eval() << endl;
    t = Expr("*", t, t);
    cout << t << " = " << t.eval() << endl;

    return 0;
}




<<C++ 沉思录>> 中文人民邮电出版 勘误

标签:c++

原文地址:http://blog.csdn.net/cinmyheart/article/details/45917741

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!