@pati_gallardo
Segmentation fault (core dumped)
C++ for Java Developers
Android Edition
Patricia Aas
JavaZone 2017
@pati_gallardo
Who am I?
@pati_gallardo
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
What Do I Want You To Remember? @pati_gallardo
Don’t do This!
Banana * b = new Banana();
EVER
@pati_gallardo
Why Should You Care? @pati_gallardo
Portable Languages
Tools of the Trade
Java
JavaScript
Python
C++
@pati_gallardo
"Here be dragons" @pati_gallardo
Interesting Parts of Android @pati_gallardo
SurfaceFlinger
The Compositor
Composes the final
frame of Android
Which consists of many
surfaces from many
clients
@pati_gallardo
Binder
IPC Mechanism around
which Android is built
Requires kernel
support
Does context switch on
IPC calls
@pati_gallardo
Zygote
Prototype App process
Uses Linux features to
isolate apps and use
memory efficiently
@pati_gallardo
Let’s Learn some Modern C++ (11-17)
@pati_gallardo
Creating An Object
@pati_gallardo
Your First Object
#include "Banana.h"
int main()
{
Banana b;
}
@pati_gallardo
Hello World
@pati_gallardo
Your First Program
#include <iostream>
#include <string>
int main()
{
std::string s("Hello ");
std::cout << s << "World!";
}
@pati_gallardo
Includes - Java Import
Including your own headers
#include "Banana.h"
Including library headers
#include <string>
@pati_gallardo
Namespaces - Java Packages
namespace fruits {
class Banana {
};
}
@pati_gallardo
fruits::Banana b;
using namespace fruits;
Banana b;
Using namespace - Java Import static
@pati_gallardo
Using Namespace
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("Hello World!");
cout << s;
}
@pati_gallardo
Values, Objects and Referring to them
@pati_gallardo
Anything Can Be a Value
int main()
{
Banana b;
int meaning = 42;
auto pi { 3.14 }
std::string s = "Hi!";
std::mutex my_mutex;
}
@pati_gallardo
Referring to Things
● Reference
● Pointer
● Value
@pati_gallardo
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
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
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
Range based For
vector<Banana> bananas;
for (auto & banana : bananas)
cout << banana;
@pati_gallardo
Const : The Many Meanings
● Object
● Function
● Local variable
● Member
● Pointer
@pati_gallardo
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
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
Parameter Passing, Return Values & Lambdas
@pati_gallardo
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
Auto return type
auto meaning() { return 42; }
@pati_gallardo
Return by Value
Banana fruit() { return Banana(); }
@pati_gallardo
Structured Bindings
std::tuple<int, int> point();
auto [x, y] = point();
@pati_gallardo
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
Memory : Lifetime & Ownership
@pati_gallardo
Where is it?
Stack
Banana b;
Heap
auto b = make_unique<Banana>();
Banana * b = new Banana();
@pati_gallardo
Stack - Similar to “Try With Resources”
Destroyed when exiting scope
Deterministic Garbage Collection
@pati_gallardo
Loving the Stack
#include <iostream>
#include <string>
using namespace std;
int main()
{
{
string s("Hello World!");
cout << s;
} // <- GC happens here!
}
@pati_gallardo
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
Structure
@pati_gallardo
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
Classes
@pati_gallardo
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
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
All Classes Are Final by Default
By extension :
All methods are final by default
@pati_gallardo
Virtual Functions
Pure virtual - Interface
virtual void bootFinished() = 0;
Use override
void bootFinished() override;
@pati_gallardo
Multiple Inheritance is Allowed
Turns out
the ‘Diamond Problem’ is mostly academic
( and so is inheritance ;) )
@pati_gallardo
All Classes Are “Abstract”
Interface : only pure virtual methods
Abstract Class : some pure virtual methods
Plain Old Class : no pure virtual methods
@pati_gallardo
Structs
A C++ Struct is a Class
Where all members are public by default
@pati_gallardo
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
Static member / function in a Class
Pretty much the same as Java
@pati_gallardo
Static Function in a Cpp File
The function is local to the file
@pati_gallardo
Static Variable in a Function
Global variable
Only accessible in this function
Same variable across calls
@pati_gallardo
Containers and Standard Types
@pati_gallardo
Use std::String - never char *
Or whatever string your project uses :D
@pati_gallardo
Std::Vector Is Great
std::vector has great performance
Don’t use raw arrays
Prefer std::vector or std::array
@pati_gallardo
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
The Standard Library & co
@pati_gallardo
Many Common Libraries
The Standard Library
Boost (Pre-Standard)
Qt (Gui and Portable)
++
Use the Libraries
@pati_gallardo
To Summarize
@pati_gallardo
Do Modern C++
Use the Stack
Use Values
Use References
Use Const
Use the Libraries
But Most Importantly...
What to Remember
@pati_gallardo
Don’t do This!
Banana * b = new Banana();
EVER
@pati_gallardo
#include "Banana.h"
int main()
{
Banana b;
}
@pati_gallardo
Make Everyone Feel Safe To Be Themselves
@pati_gallardo
C++ for Java Developers
Android Edition
Patricia Aas, Vivaldi Technologies
@pati_gallardo
Photos from pixabay.com
Vivaldi Swag
Patricia Aas, Vivaldi Technologies
@pati_gallardo
Photos from pixabay.com

C++ for Java Developers (JavaZone 2017)