Giter VIP home page Giter VIP logo

Comments (2)

pezy avatar pezy commented on August 19, 2024

那为什么还要介绍这种方法呢?在什么情况下会用到这种方法呢

首先,C++ Primer 这本书并非 Cookbook 类的书籍,它很少会提供一些实用的方法。它更多的,是引导读者去理解 C++ 语法背后的世界:

  1. 这样的语法应该如何使用 - 第一层面
  2. 为什么会有这样的语法? - 第二层面
  3. 怎么实现这样的语义,让它不再神秘。 - 第三层面

所以,我觉得看着本书,要么就是认识新语法(夯实基础),要么就是领悟基本语义(知根知底)。它并非工具书,看完都不需要留在手边,可以扔掉的。


以上是我对这本书的一点理解,回到你的问题:

那为什么还要介绍这种方法呢?在什么情况下会用到这种方法呢

这并非是某种“方法”,只是一种对 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 stringclass have
valuelike behavior. Not surprisingly, the shared_ptrclass provides pointerlike
behavior
, as does our StrBlobclass (§ 12.1.1, p. 456). The IO types and
unique_ptrdo not allow copying or assignment, so they provide neither valuelike
nor pointerlike behavior
.

那么这已经说的很明白了, 对于 13.3.2 就是为了模拟 shared_ptr 的行为。那你问我,这有啥用?为什么不用 shared_ptr ? 是不是有点本末倒置了呢?

类的两种表现,是对 Copy 语义最基本的了解。很多工程库中,会更直接的将其称为:深拷贝和浅拷贝。那么到时候你用到这些库的时候,发现它的这种抽象,再回想这一节,是不是觉得了然于心呢。

我觉得这就是它的价值所在。

from cppprimer.

husy8 avatar husy8 commented on August 19, 2024

这就是为什么这个问题不适合提交Issue。C++ Primer 确实很少涉及实际应用的例子。总觉得这样容易让人抓不着重点,谢谢啦

from cppprimer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.