The document is a presentation on C++ for Java developers. It introduces C++ concepts like classes, references, pointers, memory management and standard libraries. It emphasizes using modern C++ features like the stack instead of heap for memory, values instead of pointers, and standard libraries. It summarizes that developers should use modern C++ practices like values, references, const, and libraries, but most importantly not use raw pointers like "Banana * b = new Banana();".
4. Patricia Aas
Programmer - mainly in C++
Currently : Vivaldi Technologies
Previously : Cisco Systems, Knowit, Opera Software
Master in Computer Science - main language Java
Twitter : @pati_gallardo
26. C++ Reference &
A C++ reference is not a Java pointer
It has to reference an object
It is never null
It can never reference another object
SAFE : Banana & b
@pati_gallardo
27. C++ Pointer *
A C++ pointer is not a Java pointer
It’s often called a “Raw Pointer”
It’s a raw memory address
UNSAFE : Banana * b
@pati_gallardo
28. C++ Value
In C++ everything is a value, even objects
When passed by value it is COPIED*
Can only pass-by-value if copying is
supported*
SAFE : Banana b
* Sometimes the original value can be reused
@pati_gallardo
30. Const : The Many Meanings
● Object
● Function
● Local variable
● Member
● Pointer
@pati_gallardo
31. const - Related to Final Variables in Java
More things can be const than can be final
Java : final Banana ptr; // ptr is final
C++: Banana * const ptr; // ptr is const
C++: const Banana * ptr; // object is const
C++: const Banana * const ptr; // object & ptr is const
@pati_gallardo
32. Immutable View Of An OBJECT
const Banana & banana;
But the object is mutable - just not by you
Mark functions that don’t mutate as const
@pati_gallardo
34. Parameter Passing
Pass by const ref : const Banana & banana
Pass by reference : Banana & banana
Pass by value : Banana banana
Pass by pointer - be very careful
@pati_gallardo
38. Lambda and Captures
auto meaning = [](){ return 42; }
auto life = 42;
auto meaning = [life]() { return life; }
auto meaning = [=]() { return life; }
auto meaning = [&]() { life++; return life;}
@pati_gallardo
40. Where is it?
Stack
Banana b;
Heap
auto b = make_unique<Banana>();
Banana * b = new Banana();
@pati_gallardo
41. Stack - Similar to “Try With Resources”
Destroyed when exiting scope
Deterministic Garbage Collection
@pati_gallardo
42. Loving the Stack
#include <iostream>
#include <string>
using namespace std;
int main()
{
{
string s("Hello World!");
cout << s;
} // <- GC happens here!
}
@pati_gallardo
43. Hold a Value on the Stack that
Controls The Lifetime of Your Heap
Allocated Object
using namespace std;
auto myBanana =
make_unique<Banana>();
auto ourBanana =
make_shared<Banana>();
Smart Pointers
@pati_gallardo
45. Class Declaration in Header File
Class Definition in Cpp File
Banana.h & Banana.cpp
A header is similar to an interface
Function declarations
Member variables
@pati_gallardo
47. No Explicit Root Superclass
Implicitly defined member functions
Don’t redefine them if you don’t need to
The defaults are good
Rule-of-zero
(Zero except for the constructor)
@pati_gallardo
48. class B
{
public:
// Constructor
B();
// Destructor
~B();
// Copy constructor
B(const B&);
// Copy assignment op
B& operator=(const B&);
// Move constructor
B(B&&);
// Move assignment op
B& operator=(B&&);
};
Implicitly Defined
Functions
@pati_gallardo
49. All Classes Are Final by Default
By extension :
All methods are final by default
@pati_gallardo
50. Virtual Functions
Pure virtual - Interface
virtual void bootFinished() = 0;
Use override
void bootFinished() override;
@pati_gallardo
51. Multiple Inheritance is Allowed
Turns out
the ‘Diamond Problem’ is mostly academic
( and so is inheritance ;) )
@pati_gallardo
52. All Classes Are “Abstract”
Interface : only pure virtual methods
Abstract Class : some pure virtual methods
Plain Old Class : no pure virtual methods
@pati_gallardo
53. Structs
A C++ Struct is a Class
Where all members are public by default
@pati_gallardo
54. Static : The Many Meanings
“There can be only one”
Function in cpp file
Local variable in a function
Member / function in a class
@pati_gallardo
55. Static member / function in a Class
Pretty much the same as Java
@pati_gallardo
56. Static Function in a Cpp File
The function is local to the file
@pati_gallardo
57. Static Variable in a Function
Global variable
Only accessible in this function
Same variable across calls
@pati_gallardo
61. Use std::Algorithms
using namespace std;
vector<int> v { 1337, 42, 256 };
auto r = find_if(begin(v), end(v), [](int i){
return i == 42;
});
@pati_gallardo