Last active
January 2, 2016 18:00
-
-
Save jayd3e/ea679eef2c81cca091a0 to your computer and use it in GitHub Desktop.
C++ Notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int a = 0; | |
// In C++ there is copy initialization | |
int b = a; | |
// And direct initialization | |
int c(0); | |
// They are not the same, and have different behavior. People tend to favor | |
// copy initialization, except for direct initialization in a few cases. | |
// See this SO answer http://stackoverflow.com/a/4293816 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Calling a function directly followed by <> causes the template(generic) to use the default | |
// type definitions for arguments | |
newTaskScheduled.wait_until<>(lock, taskQueue.begin()->first) | |
// http://stackoverflow.com/a/20398693 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Use the area after the colon of a constructor to: | |
1. Calling base class constructors | |
2. Initialising member variables before the body of the constructor executes | |
See here for more details: http://stackoverflow.com/a/2785639 | |
*/ | |
// Example: | |
CScheduler::CScheduler() : nThreadsServicingQueue(0), stopRequested(false), stopWhenEmpty(false) { | |
} | |
// Note: the syntax for modifying member variables in the initialization list is like calling a function. This was something | |
// that was confusing for me at first. | |
// More can be found here http://www.cprogramming.com/tutorial/initialization-lists-c++.html. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A double colon is used when accessing static members of classes/namespaces | |
class A { | |
public: | |
static void StaticFunction() { } | |
void NonStaticFunction() { } | |
int variable; | |
}; | |
int main() { | |
StaticFunction(); // this is an error, because 'StaticFunction' doesn't exist in the global namespace | |
A::StaticFunction(); // this is OK, because now the compiler knows to look in the A class to find StaticFunction. | |
// in order to call non-static functions or to access variables, we need an object | |
A obj; // 'obj' is our object | |
obj.variable = 5; // here we use the . operator because we have an object. No need for the :: operator | |
// because the scope is implied by the object type ('obj' is an 'A', so it knows to look in 'A' for 'variable') | |
obj.NonStaticFunction(); // same thing with member function calls | |
} | |
// So in conclusion, use :: when accessing a member from a static class, use . when accessing a member | |
// from an object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can use pre-processor directives to replace sections of the code | |
#define TABLE_SIZE 100 | |
int table1[TABLE_SIZE]; | |
int table2[TABLE_SIZE]; | |
// Turns into | |
int table1[100]; | |
int table2[100]; | |
// #ifdef & #ifndef adds or discards sections of the code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment