Comments (2)
那为什么还要介绍这种方法呢?在什么情况下会用到这种方法呢
首先,C++ Primer 这本书并非 Cookbook 类的书籍,它很少会提供一些实用的方法。它更多的,是引导读者去理解 C++ 语法背后的世界:
- 这样的语法应该如何使用 - 第一层面
- 为什么会有这样的语法? - 第二层面
- 怎么实现这样的语义,让它不再神秘。 - 第三层面
所以,我觉得看着本书,要么就是认识新语法(夯实基础),要么就是领悟基本语义(知根知底)。它并非工具书,看完都不需要留在手边,可以扔掉的。
以上是我对这本书的一点理解,回到你的问题:
那为什么还要介绍这种方法呢?在什么情况下会用到这种方法呢
这并非是某种“方法”,只是一种对 Classes that act like pointers 的模拟。所以你问,什么情况用,就更无从答起了。甚至,整个 13 章, 除了 Big 5 怎么写,比较有实用价值外,真的不会有什么情况会“用”到了。
这一节介绍了对两种类的模拟,其一是 act like values,另外就是你说的 act like pointers. 小节一开始就有一段综述(介绍 STL 出现过的类行为):
Of the library classes we’ve used, the library containers and
string
class have
valuelike behavior. Not surprisingly, theshared_ptr
class provides pointerlike
behavior, as does ourStrBlob
class (§ 12.1.1, p. 456). The IO types and
unique_ptr
do not allow copying or assignment, so they provide neither valuelike
nor pointerlike behavior.
那么这已经说的很明白了, 对于 13.3.2 就是为了模拟 shared_ptr
的行为。那你问我,这有啥用?为什么不用 shared_ptr
? 是不是有点本末倒置了呢?
类的两种表现,是对 Copy 语义最基本的了解。很多工程库中,会更直接的将其称为:深拷贝和浅拷贝。那么到时候你用到这些库的时候,发现它的这种抽象,再回想这一节,是不是觉得了然于心呢。
我觉得这就是它的价值所在。
from cppprimer.
这就是为什么这个问题不适合提交Issue。C++ Primer 确实很少涉及实际应用的例子。总觉得这样容易让人抓不着重点,谢谢啦
from cppprimer.
Related Issues (20)
- ex16_29 include error file,It compile failed HOT 2
- about 16.2.7 HOT 2
- Exercise 7.36
- Exercise 6.53
- Exercise 5.3 correct answer HOT 1
- Ex 6_8 HOT 1
- Ex 6_9 (added #include "fact.cpp")
- Ex 6_10 (added "return 0;")
- Exercise 13.37
- Answer to Exercises 3.37 is not entirely true.
- Ex16.49
- The new soulution of Exercise 5.12
- Ex.7.53 will not compile under the C++14 standard
- #include<exception>
- ex7_22.h 'class' instead of 'struct'
- Bug report exercise 16.12
- An error in exercise 16.28, in file <unique_pointer.h>
- Small modification for answer of Exercise 3.9 - mention `s` is a empty string
- Exercise 9.28(The loop may not stop)
- terminate called after throwing an instance of 'std::bad_alloc
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppprimer.