Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

练习15.11 #106

Closed
susong opened this issue Mar 15, 2017 · 2 comments
Closed

练习15.11 #106

susong opened this issue Mar 15, 2017 · 2 comments

Comments

@susong
Copy link

susong commented Mar 15, 2017

运行答案中代码(第一段):

    Quote& r = q;
    r.debug();
	std::cout << "\n";
    r = bq;
    r.debug();
	std::cout << "\n";
    r = lq;
    r.debug();
	std::cout << "\n";

输出为:

data members of this class:
bookNo= aaa price= 10.6 
data members of this class:
bookNo= bbb price= 111 
data members of this class:
bookNo= ccc price= 222 

我添加如下形式的代码(第二段):

Quote &r1 = q;
r1.debug();
std::cout << "\n";
Quote &r2 = bq;
r2.debug();
std::cout << "\n";
Quote& r3 = lq;
r3.debug();
std::cout << "\n";

输出为:

data members of this class:
bookNo= aaa price= 10.6 
data members of this class:
bookNo= bbb price= 111 min_qty= 10 discount= 0.3 
data members of this class:
bookNo= ccc price= 222 max_qty= 10 discount= 0.3 

我添加如下形式的代码(第三段):

Quote &r4 = lq;
r4.debug();
std::cout << "\n";
r4 = bq;
r4.debug();
std::cout << "\n";
r4 = q;
r4.debug();
std::cout << "\n";

输出为:

data members of this class:
bookNo= ccc price= 222 max_qty= 10 discount= 0.3 
data members of this class:
bookNo= bbb price= 111 max_qty= 10 discount= 0.3 
data members of this class:
bookNo= aaa price= 10.6 max_qty= 10 discount= 0.3 

我添加如下形式的代码(第四段):

Quote &r = q;
r.debug();
std::cout << "\n";
r = bq;
r.debug();
std::cout << "\n";
r = lq;
r.debug();
std::cout << "\n";
Quote &r5 = q;
r5.debug();
std::cout << "\n";

输出为:

data members of this class:
bookNo= aaa price= 10.6 
data members of this class:
bookNo= bbb price= 111 
data members of this class:
bookNo= ccc price= 222 
data members of this class:
bookNo= ccc price= 222 

上面四段代码是一段段单独运行的。
为什么第一段代码中,bq,lq的两个属性没有输出?
为什么第三段代码中,q会输出他没有的两个属性的值?
为什么第四段代码中,r5会输出lq的值?
为什么会这样,还请麻烦解释一下,谢谢!!!

@pezy
Copy link
Owner

pezy commented Mar 16, 2017

为什么第一段代码中,bq,lq的两个属性没有输出?

因为都是 r 调用 debug, 而 r 是 Quote.

为什么第三段代码中,q会输出他没有的两个属性的值?

麻烦认真点, 第三段里调用 debug 输出的是 r4, 这里发生了动态绑定,r4 实际是 Limit_quote.

为什么第四段代码中,r5会输出lq的值?

当然啦, 你把 lq 赋值给 r, r 就是 q,r5 也就是 q, r5 输出的值当然就是 lq 的值。

为什么会这样,还请麻烦解释一下,谢谢!!!

为什么会这样, 好好看书就明白了!!!

@susong
Copy link
Author

susong commented Mar 16, 2017

谢谢您的解答,果然是我看书不够细心,麻烦您了,谢谢!!!

@pezy pezy closed this as completed Mar 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants