c plus plus interview review

这是我看了《C笔试面试宝典》一书的笔记 new, delete, malloc, free的关系 new和delete是C++的 运算符 ,new调用构造函数,delete调用析构函数

improve code readability

这是我读 “the art of readable code” 一书做的笔记 简化循环和逻辑 making control flow easy to read 下面的代码: if (length >= 10) 要比: if (10 <= length) 更容易看懂。这是很显然的。而在C语言中,有的人为了

reStructuredText语法笔记

reStructuredText用于排版非常好。但是,和C++一样,越是灵活,越是可扩展,越是复杂。 有些语法,你永远记不住,我经常遇到一些表

A tour to The Badaling Great Wall

Today, I went to the Badaling Great Wall alone. There is a saying that He who has never been to the Great Wall is not a true man. I had read this saying for many times when I was in high school, but I have never been to the Great Wall util today. I got up at 7:00am and began my tour at about 7:40am after the breakfast. It took me 30 minutes to arrive at Xi zhi men station from where I live.

About Constructors Destructors and Assignment Operators

What functions C++ will silently write and call? If we declare an empty class, the compiler will declare a constructor, a copy constructor, a copy assignment operator and a destructor for us. class Girl { }; But the compiler is not foolish, it will only generate them when needed. What do the generated functions do? For construtor, it will invoke the corresponding constructor and non-static members of its base class.

Always pass parameters by reference-to-const

We all know that pass by value is not effecient.So we use pass by pointer in C.However, in C++, we prefer pass by referece-to-const. Here is an example. #include <iostream> #include <cstdio> class Dog { public: Dog() { printf("Calling Dog's constructor\n"); } Dog(const Dog& d) { printf("Calling Dog's copy constructor\n"); this->name = d.name; } virtual ~Dog() { printf("Calling Dog's destructor\n"); } private: std::string name; }; void walk_the_dog(Dog d) { } int main(int argc, char **argv) { Dog dog; walk_the_dog(dog); return 0; } The running result is as follows:

Const anyway!

Const pointer const is versatile.I am always confused by the following syntax. const char *str = "Hello"; // const data, non-const pointer char * const str = "Hello"; // const pointer, non-const data Now there is a tip to remember it. If const appears to the right of the asterisk, the pointer is constant.If const appears to the left of the asterisk, the data is constant. So the following two statements are the same.

Cpp Resource Management

I always forget freeing a memory that allocated from the heap.And I always forget closing the file handler. Resources include file descriptors, mutex locks, fonts, brushes, database connections and network sockets. I think less programmers can remember closing a file handler, especially in a large project. Nowadays, many programming languages have the garbage collection function. In C++, since the destructor will be automatically called, we can release the resources automatically if we put the resource into an object.

Do not hide inherited names

In inheritance, there are some rules defined by C++, one of which is that the virables or the functions in the base class will be hidden if there are virables and functions with the same name. It seems perfect, but it’s a pitfall of C++. Consider the following example. #include <cstdio> #include <iostream> class Person { public: explicit Person() { } ~Person() { } void sleep() const { printf("Person sleep\n"); } void sleep(const int sec) const { printf("Person sleep %d s\n", sec); } private: Person(const Person&); const Person& operator=(const Person&); }; class Student:public Person { public: explicit Student() { } ~Person() { } void sleep() const { printf("Student sleep\n"); } private: Student(const Student&); const Student& operator=(const Student&); }; int main(int argc, char **argv) { Student stu; stu.