C++ Primer Notes [to be continued..]

The type determines the amount of storage that is allocated for the variable and the set of operations that can be performed on it.
 
 
 int ival(1024); // direct-init
 int ival = 1024; // copy-init
 
 initialization is not assignment
 
 direct syntax is more flexible and can be slightly more efficient
 
 
 
 
 
 never rely on undefined behavior
 
 no compiler can detect all uses of uninitialized variables
 
 uninitialized variables actually do have a value
 
 in order for multiple files to access the same variable, C++ distinguishes between declarations and definitions
 
 extern: definitioin of a var exists elsewhere in the program. A variable could be declared multiple times in a program, but must be defined only once
 
 A name can be reused as long as it is used in different contexts, from which the different meanings of the name can be distinguished. SCOPE
 A name can refer to different entities in different scopes
 
 for (int i = 1; i < 10; i++) ... statement scope only
 
 the definition of local variables HIDEs the global var with the same name, always bad idea to do so
 
 unlike other variables, const variables declared at global scope are local to the file in which the object is defined. The variable exists in that file only and cannot be accessed by other files.
 
 Nonconstant variables are extern by default. Const must explicty specify as extern in order to be accessiable from other files.
 
 constant expression: const int a = 3; const int a = someConst;
 when a const is initialized by a value that is not a constant expression, then it should not be defined in header file. Should be in a source file, with extern
 
 the #include facility is a part of the C++ preprocessor, which manipulates the source text of our programs and runs before the compiler.
 
 
 To avoid name clashes, preprocessor variables usually are written in all uppercase letters.
 
 headers should have guards
 
 
 angle brackets <> standard header
 curly braces {}
 
 
 DO NOT use namespaces in headers!!!
 
 Although we acn preallocate a given number of elements in a vetor it is usually more efficient to define an empty vector and add elements to it.
 
 
 An iterator is a type that lets us examine the elements in a container and navigate from one element to another

P260 Introducing This

To understand a member function call, we might think that we we write:

total.same_isbn(trans);it is as if the compiler rewrites the call as:

Sales_item:: same_isbn(&total, trans); //<--- hmm, this is actually the C style code.

// Also reminds me Python object method always have a self ref

In this call, the data member isbn inside same_isbn is bound to the one belonging to total.

-----------

Classes in C++Control what happens when objects are initialized, copied, assigned, and destroyed. In this respect, C++ differs from many other languages, many of which do not give class designers the ability to control these operations.

pointers to const objects:

  const double pi = 3.13;

  const double *cptr = &pi; // cptr may point to a double that is const, i.e. points to const double. Could point to other const double

pointers that are themselves const:

  int errNum = 0;

  int *const curErr = &errNum;

  

typedef string *pstring;

const pstring cstr;

Equals to: string *const cstr; // it is the pointer that is constant

NOT

const string *cstr; // not the object that points to is consant

Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called. A constant member function cannot modify any non-static data members or call any member functions that aren‘t constant.

getters, or avg_price P261

It is a good idea to write constructor initializer in the same order as the members are declared. Moreover, when possible, avoid using members to initialize other members.

C16 Templates and Generic Programming

In C++, templates are the foundation for generic programming. A template is a blueprint or formula for creating a class or a function.

Once the compiler determines the actual template argument(s), it instantiates an distance of the function template for us. Having deduced the actual template arguments, it generates and compiles a version of the function.

C5 Expression

Setting the pointer to 0 after the object it refers to has been deleted makes it clear that the pointer points to no object.

dangling pointer: one that refers to memory that once held an object but does so no longer, cuz it still continas the address of the object to which it pointED (on many machines)

C1 Getting Started

In fact, a primary focus of the design of C++ is to make it possible to define class types that behave as naturally as the built-in type themselves.

时间: 2024-10-05 04:11:12

C++ Primer Notes [to be continued..]的相关文章

Unity 官方教程 学习

Interface & Essentials Using the Unity Interface 1.Interface Overview https://unity3d.com/cn/learn/tutorials/topics/interface-essentials/interface-overview?playlist=17090 2.The Scene View https://unity3d.com/cn/learn/tutorials/topics/interface-essent

Notes for Matrices (To be continued)

Matrix Multiplication: The meaning of $Ax$ (range of $A$) and $Ax=b$. Most students, after finishing the course linear algebra, may don't understand the matrix multiplication yet. Here I will show the readers, roughly speaking, the real and the most

C++ Notes 1 - size_type - Accelerated Ch3

1. 为什么用string::size_type而不是int? --Why use string::size_type ? int is supposed to work! it holds numbers! --A short holds numbers too. So does a signed char. But none of those types are guaranteed to be large enough to represent the sizes of any strin

C++ Primer快速学习 第一章 入门

很多人说C++Primer不适合于入门,本系列入门文章向大家证明了:这是一个谎言. 第一章 入门 本章介绍 C++ 的大部分基本要素:内置类型.库类型.类类型.变量.表 达式.语句和函数. 1.1. 编写简单的 C++ 程序 每个 C++ 程序都包含一个或多个 函数 ,而且必须有一个命名为 main.函数 由执行函数功能的语句序列组成.操作系统通过调用 main 函数来执行程序, main 函数则执行组成自己的语句并返回一个值给操作系统. 下面是一个简单的 main 函数,它不执行任何功能,只是

84. 从视图索引说Notes数据库(下)

作用和代价上文介绍了关系型数据库里的索引.Notes数据库里的索引隐藏在视图概念里(本文的讨论只针对Notes的视图索引,不包含全文索引.).开发人员创建的视图仅仅是存放在数据库里的一条设计文档,数据库引擎会依据它创建和更新索引.关系型数据库里的索引是从记录中抽取的数据排序而组成的数据结构(主要是B树),Notes视图的索引还包括未排序的列.计算值.分类.总计等等数据(数据结构仍然是B树,如果运气足够好的话,你会遇到Notes报出B-tree structure is invalid的错误).用

C++ Primer Plus的若干收获--(八)

接下我会比较系统的介绍OOP的核心--类这个概念.可能会写的比较繁琐,比较多,但是自己理解总是好的.还记得上学期刚开始的时候老师讲的数据结构,一上来就来个LIst的一个大类,结果全班就傻了,完全不知所云(ps:博主是数学系,只学过C),然后自己就坐在图书馆借了两大本C++书开始啃了起来,大概啃了两个月才把C++学了个大概,尤其是读到类这块都是三四百行的常常的代码,看的那叫一个痛苦啊.不过自此之后就真正喜欢上这门语言了,再到之后能用C++写上个一两千行的代码,才发现之前的努力还是有收获的.好了,废

C++ Primer笔记8_动态内存_智能指针

1.动态内存 C++中,动态内存管理是通过一对运算符完成的:new和delete.C语言中通过malloc与free函数来实现先动态内存的分配与释放.C++中new与delete的实现其实会调用malloc与free. new分配: 分配变量空间: int *a = new int; // 不初始化 int *b = new int(10); //初始化为10 string *str = new string(10, ); 分配数组空间: int *arr = new int[10];//分配的

C++ Primer 学习笔记_98_特殊工具与技术 --优化内存分配

特殊工具与技术 --优化内存分配 引言: C++的内存分配是一种类型化操作:new为特定类型分配内存,并在新分配的内存中构造该类型的一个对象.new表达式自动运行合适的构造函数来初始化每个动态分配的类类型对象. new基于每个对象分配内存的事实可能会对某些类强加不可接受的运行时开销,这样的类可能需要使用用户级的类类型对象分配能够更快一些.这样的类使用的通用策略是,预先分配用于创建新对象的内存,需要时在预先分配的内存中构造每个新对象. 另外一些类希望按最小尺寸为自己的数据成员分配需要的内存.例如,

C++ Primer 学习笔记_73_面向对象编程 --再谈文本查询示例

面向对象编程 --再谈文本查询示例 引言: 扩展第10.6节的文本查询应用程序,使我们的系统可以支持更复杂的查询. 为了说明问题,将用下面的简单小说来运行查询: Alice Emma has long flowing red hair. Her Daddy says when the wind blows through her hair, it looks almost alive, like a fiery bird in flight. A beautiful fiery bird, he