background preloader

C++

Facebook Twitter

[C++] Client / Server Tutorial. Task Class (Concurrency Runtime) Functions with Variable Argument Lists (C++) RAII and smart pointers in C++ Preprocessor directives - C++ Documentation. Preprocessor directives are lines included in the code of programs preceded by a hash sign (#).

Preprocessor directives - C++ Documentation

These lines are not program statements but directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements. These preprocessor directives extend only across a single line of code. As soon as a newline character is found, the preprocessor directive is ends. No semicolon (;) is expected at the end of a preprocessor directive. Macro definitions (#define, #undef) To define preprocessor macros we can use #define. #define identifier replacement When the preprocessor encounters this directive, it replaces any occurrence of identifier in the rest of the code by replacement. After the preprocessor has replaced TABLE_SIZE, the code becomes equivalent to: #define can work also with parameters to define function macros: This would generate the same code as: For example:

Pointer / Array syntax (char **p, *p[n]) in C/C++ Resource Acquisition Is Initialization. Benefits[edit] The advantages of RAII as a resource management technique are that it provides encapsulation, exception safety (for stack resources), and locality (it allows acquisition and release logic to be written next to each other).

Resource Acquisition Is Initialization

Resource management therefore needs to be tied to the lifespan of suitable objects in order to gain automatic allocation and reclamation. Resources are acquired during initialization, when there is no chance of them being used before they are available, and released with the destruction of the same objects, which is guaranteed to take place even in case of errors. Typical uses[edit] Another typical example is interacting with files: We could have an object that represents a file that is open for writing, wherein the file is opened in the constructor and closed when execution leaves the object's scope. C++ example[edit] The following C++11 example demonstrates usage of RAII for file access and mutex locking: Notes[edit] References[edit]

Basics of C++ Namespace. Forward: In this part of the series, I talk about the basics of C++ Namespace.

Basics of C++ Namespace

By: Chrysanthus Date Published: 7 Feb 2013 Introduction This is part 1 of my series, C++ Namespace. In this part of the series, I talk about the basics of C++ Namespace. My volume, C++ Course (Taking the Bull by the Horns) is divided into two portions. Back to the story at hand: Basics of C++ Namespace. PrerequisiteThis series is part of my C++ Course volume. A problemRead and try the following code: int myInt = 3;int yourInt = 5; int herInt = myInt + yourInt; int main() { return 0; } The compilation of the above code went well. #include <iostream> using namespace std; These two lines especially the second one are related to the namespace concept.

Note, a program can never be executed unless it is compiled successfully. Now, read and try the following code where another identifier is defined (the code will not compile and will issue error messages – note the error messages issued). Int myInt; <a href=" Chrys. Cplusplus.com - The C++ Resources Network.

Memory Management