C++, recent features and good programming practice

0 downloads 0 Views 22MB Size Report
int *ptr_arr; ptr_arr=(int*) malloc(10*sizeof(int));. // ... free(ptr_arr);. C++. // Avoiding pointers .... C++ templates are Turing-complete! ..... python api ... Google C++ Style Guide [link]. High integrity C++ [link]. Lockheed Martin [link] .... (brown.edu).
C++, recent features and good programming practice Arash Mohammadi arash.m [at] research.deakin.edu.au

Deakin University Institute for Intelligent Systems Research and Innovation (IISRI)

C and C++ C

C++

Dennis Ritchie (1941-2011)

Bjarne Stroustrup (1950)

Developed in 1972 Notable release: C89

Developed in 1985 Notable releases: C++98, C++11, C++14, C++17 Drafted: C++20

C and C++ C

C++

// Undesired pointers void update(Status *device);

// Avoiding pointers void update(Status &device);

// Strings hard to use char msg[100]; strcpy(msg,"Hello"); strcat(msg," world");

// Strings easy to use string msg; msg="Hello"; msg+=" world";

// Manual memory release int *ptr_arr; ptr_arr=(int*) malloc(10*sizeof(int)); // ... free(ptr_arr);

// Automatic memory release vector my_arr; // ...

Resource Acquisition Is Initialization (RAII) Manual release int read_codec() { int *buffer=new int[1000]; int error_code; error_code=read_header(buffer); if(error_code) { delete[] buffer; return error_code; } error_code=read_body(buffer); if(error_code) { delete[] buffer; return error_code; } error_code=read_tail(buffer); delete[] buffer; return error_code; }

Automatic release struct BufferType { int *p; BufferType(){p=new int[1000];} ~BufferType(){delete[] p;} }; void read_codec() { BufferType buffer; read_header(buffer); read_body(buffer); read_tail(buffer); } // ... try{ read_codec(); } catch(const exception& e) { cout

Suggest Documents