Shared Pointer A shared pointer is a smart pointer that allows for distributed ownership of data. Each time it is assigned, a reference count is incremented, indicating one more owner of the data. When the pointer goes out of scope or an owner calls reset , the reference count is decremented. When the reference count goes to 0, the data pointed to is deallocated. Use make_shared : auto person = std :: make_shared < Person > ( ) ; if ( person ) { cout << "Person's address is " << person -> address ; } C++17 allows use of shared pointers to point to dynamically allocated arrays. However, make_shared cannot be used. Below is an example: shared_ptr < Person [ ] > persons ( new Person [ 10 ] ) ; persons [ 0 ] . _name = "Jack Sparrow" ; persons [ 0 ] . _address = "Caribbean" ; cout << "Address of first person " << persons [ 0 ] . address << endl ; A better solutio...
C++ Tour Hello World Here is the hello world program for C++: # include <iostream> int main ( ) { std :: cout << "Hello, World" << std :: endl ; return 0 ; } Building a C++ Program Building a C++ program is a three step process: Code is run through preprocessor which processes meta information The code is compiled where each .cpp file is translated to .o object file Individual object files are linked together into a single application. Main function Main function is where program execution starts. It comes in two flavors: with and without arguments. With arguments, it looks as follows: int main ( int argc , char * argv [ ] ) where argc gives the number of commandline arguments to the program, and argv array contains those arguments. The actual arguments start from index 1. I/O Streams cout is the stream for standard output, cerr is the strea...
Selecting HTML document with JavaScript Given the following HTML: <!DOCTYPE html> < html lang = " en " > < head > < meta charset = " UTF-8 " > < meta name = " viewport " content = " width=device-width, initial-scale=1.0 " > < title > My Website </ title > </ head > < body > < h1 id = " title " > Hello </ h1 > < input type = " checkbox " > < button class = " btn " > Click Me </ button > < ul id = " list " > < li class = " item " > < a href = " https://www.google.com " > Google </ a > </ li > < li class = " item " > Second </ li > < li class = " item " > Third </ li > </ ul > </ body > < footer > < script ...
Comments
Post a Comment