Valid C ++ Reading Notes 1

Source: Internet
Author: User

TMP template metaprogramming

 

0. The explicit constructor is better than the non-explicit constructor.

1. You can use const instead of # define to define a constant.

# Define has no scope or encapsulation.

Class A <br/>{< br/> PRIVATE: <br/> static const int num = 5; <br/> // when the constant is static and an integer type, you can skip the definition. </P> <p >}; <br/> const int A: num; <br/> // num definition. The initial value has been obtained in the declarative statement, so the initial value does not need to be given in the definition.

// When a fixed-length array needs to be declared in the class, the old compiler cannot assign an initial value in the class, therefore, use Enum <br/> Class A <br/>{< br/> PRIVATE: <br/> Enum {num = 5 }; <br/> int array [num]; <br/> };

However, Enum cannot obtain the address, while const constants can obtain the address. Therefore, pointers or references cannot point to Enum constants.

 

# When define defines a macro, it can be replaced by the template inline function.

# Ifdef/# ifndef is very important.

 

Const vector <int>: iterator Pos; // similar to T * const, The point is unchanged.

Vector <int>: const_iterator Pos; // elements similar to const T * do not change.

 

The Return Value of the function is followed by const to avoid exceptions.

Const int operator + (const A, const a B) <br/>{< br/> return. num () + B. num (); <br/>}< br/> int C; <br/> A + B = C; // if no const is added to the function, the value can be incorrectly assigned. <Br/> C = a + B; <br/> if (a + B = C) // const can avoid this error.

 

Member functions can be overloaded if their constants are different.

The compiler follows the bitwise constness criterion. If you want to implement logical constness, use mutable to change the const value.

Bitwise constness: as long as the member function is declared as const, no member in the class can be changed.

Logical constness: Never changed for the customer.

 

When you reload a member function (only different from the const function), it is better to call the const function instead of the const function. Forced conversion can be performed in the middle.

 

Consider const when writing a function.

When the member variables are const or reference, they must initialize the list instead of assigning values.

The constructor initializes the object with the initialization list.

When the initialization efficiency of the constructor is similar to the assignment efficiency, the private function is created to store the common part and called in multiple constructor.

When two classes are in different compilation units and one class uses another class object, the initialization sequence is unknown. To ensure the sequence, a function must be defined.

# Include ". H "<br/> Class A <br/>{< br/> Public: <br/> size_t numdisk () const; </P> <p> }; <br/> # include "B. H "<br/> Class B <br/> {<br/> Public: <br/> // B (Params); </P> <p> }; <br/>/* <br/> B: B (Params) <br/> {<br/> size_t disk =. numdisk (); // do not know whether a has been initialized during the call <br/>}< br/> */

When it changes to the following,

/* A & ()
{
Static;
Return;
}
*/

The situation has improved.

This is the legendary Singleton model.

Class filesystem <br/>{< br/> Public: <br/> size_t numdisk () const; <br/>}; <br/> extern filesystem TFs; <br/> class directory <br/>{< br/> Public: <br/> directory (Params); </P> <p> }; <br/> directory: Directory (Params) <br/>{< br/> size_t disks = TFs. numdisk (); <br/>}< br/>

Ultimate Edition

Class filesystem <br/>{}; <br/> filesystem & TFS () <br/>{< br/> static filesystem FS; <br/> return FS; <br/>}< br/> class directory <br/>{}; <br/> directory: Directory (Params) <br/>{< br/> size_t disks = TFS (). numdisk (); <br/>}< br/> directory & tempdir () <br/>{< br/> static directory TD; <br/> return TD; <br/>}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.