Skip to content

Instantly share code, notes, and snippets.

@vonnenaut
Last active January 10, 2021 10:48
Show Gist options
  • Save vonnenaut/5869d04716983a4ca302f7d94f0658e0 to your computer and use it in GitHub Desktop.
Save vonnenaut/5869d04716983a4ca302f7d94f0658e0 to your computer and use it in GitHub Desktop.
C++

C++

  • very large language, difficult to learn

  • has 5 parts:

    • C language
    • C preprocessor
    • Classes and Objects
    • Templates
    • Standard Template Library
  • passing objects onto the stack is bad practice; instead use pointers or references

    • more secure
    • less likely to cause bugs or crashes
    • more efficient, conserving resources and speeding up programs
    • static modifier is used to place variables on heap instead of stack
    • additionally, const helps ward off unintended side-effects
  • :: is scope resolution operator, which is used to differentiate between variables of the same name of different scopes, to define a function outside a class, to access a class' static variables, to deal with issues relating to multiple inheritance, for namespace, to refer to a class inside another class

  • const functions can always be called

  • non-const functions can only be called by mutable (non-const) objects

  • Destructors are named starting with a tilde (~)

 

Namespaces

  • Use the standard C++ namespace using namespace std;

  • Use only part of a namespace: using std::cin;

  • Including header files: #include <iostream>

    • old way: #include <iostream.h>

 

Pointers

Using Pointers With Arrays Example
#include <iostream>
using namespace std;

int main() {
  int luckyNumbers[5] = { 1, 3, 5, 7, 9 };
  cout << luckyNumbers << endl;      // address of first element in array
  cout << &luckyNumbers[0] << endl;  // same, written differently
  cout << luckyNumbers[0] << endl;   // value of first element in array 
  cout << *luckyNumbers << endl;     // same, using dereferencing syntax (*varName)

  return 0;
}

 

References

  • an alias, another name for an already-existing variable

Things References Cannot Do that Pointers Can

  • can't have NULL references, must always point to legitimate storage location w/a value
  • once initialized, a reference cannot be changed to point to another object
  • a reference must be initialized to a value when created, making wild references (pointing to arbitrary memory location) less likely
  • references only have one level of indirection, i.e., a reference to a variable, whereas pointers can have multiple levels, i.e., a pointer to a pointer to a variable, etc.

Reasons to Use a Reference

  • as with pointers, set up a function/method to utilize pass-by-reference in order to persistently change the value of variables after the function/method block is cleaned up/ends
  • as with pointers, save resources by passing a reference to a structure instead of having to copy and pass the entire contents of the structure to a function/method
  • can be used in for-each loops to modify all objects in an iterable
    • the previous point applies here as well - saves resources by not having to copy structures, instead simply copying references to them
  • do the above in a more restricted and safe manner than w/pointers
Reference Example
#include<iostream>
using namespace std;
 
int main()
{
  int x = 10;
 
  // ref is a reference to x.
  int& ref = x;
 
  // Value of x is now changed to 20
  ref = 20;
  cout << "x = " << x << endl ;
 
  // Value of x is now changed to 30
  x = 30;
  cout << "ref = " << ref << endl ;
 
  return 0;
}

 

Function Pointers

  • somewhat rare but very useful
Function Pointer Example
#include <cstdio>

void func()
{
    puts("this is func()");
}

int main()
{
    puts("this is main()");
    void (*pfunc)() = func;
    (*pfunc)();  // can also use pfunc() but it's ambiguous and not recommended

    return 0;
}

 

Variadic Functions

  • allow any number of arguments to be passed
  • require #include <cstdarg>
Variadic Function Example
#include <cstdio>
#include <cstdarg>

double average(const int count, ...)
{
    va_list ap;
    int i;
    double total = 0.0;
    
    va_start(ap, count);
    
    for(i = 0; i < count; ++i) {
        total += va_arg(ap, double);
    }
    
    va_end(ap);
    
    return total / count;
}

 

Further Information

https://www.geeksforgeeks.org/references-in-c/

https://www.geeksforgeeks.org/scope-resolution-operator-in-c/#:~:text=In%20C%2B%2B%2C%20scope%20resolution,can%20access%20a%20global%20variable

https://grpc.io/docs/languages/cpp/quickstart/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment