Walking into the magic C ++ Simple version

zhaozj2021-02-11  148

The C program has written more than 5 years, and constantly learning, the more you understand, the less you know. However, learning C is also easy to walk into the magic, let me take a few examples: 1. All grammar is understood, it is easy to produce the mentality of cattle B. "Oh, this is very cool", in fact, it does not have to kill chicken with cow knife It will only make efficiency decrease. EX: Class A {public: a (); virtual ~ a ();}; what Virtual does not inherit?

2. After found C easy to transplant, began to brag "I want to transplant this code to Linux!" In fact, it is clearly the code of For DirectX, how to transplant?

3, clearly can solve problems with an int, and use Template. EX: Template T TestFunc (t i) {... // Some else} may also be more exclusive: template <> int testfunc (int i) {... // some else} is not clear Int TestFunc (INT I) {....}, unnecessary templates will only increase compilation time and code expansion. Maybe you will say: I can use Explicit Instance, please.

4, clearly can use Operator New, must use Placement Operator new.ex: class a {public: void * operator new (size_t, void *); void operator delete (void *, size_t);}; unless necessary, most It is not necessary to use the Placement Operator New, because of this, the boundary must be manually adjusted when allocating memory, and must be explicitly destructed, then release memory: void foo (class_name * p) {new (p) a;} void foo_destroy (class_name * p) {p-> ~ class_name (); delete p; // A :: Operator delete (p, sizeof (a))} is more troublesome unless it is necessary.

....... Waiting so ...

C is a profound thing, the abuse characteristics will only reduce efficiency, and the correct written elegant code will make C shine.

转载请注明原文地址:https://www.9cbs.com/read-5963.html

New Post(0)