SlideShare una empresa de Scribd logo
1 de 53
Descargar para leer sin conexión
@pati_gallardo
C++ for Java Developers
@pati_gallardo Patricia Aas
SweedenCpp Meetup C++ Stockholm 0x08 2017
Patricia Aas - Vivaldi Browser
Programmer - mainly in C++
Currently : Vivaldi Technologies
Previously : Cisco Systems, Knowit, Opera Software
Master in Computer Science - main language Java
Twitter : @pati_gallardo
Let’s Learn some Modern C++ (11-17)
@pati_gallardo
Don’t do This!
Hero * h = new Hero();
EVER
@pati_gallardo
Your First Object
#include "Hero.h"
int main()
{
Hero h;
}
@pati_gallardo
Your First Program
#include <iostream>
#include <string>
int main()
{
std::string s("Wonder ");
std::cout << s << "Woman!";
}
@pati_gallardo
Includes - Java Import
Including your own headers
#include "Hero.h"
Including library headers
#include <string>
@pati_gallardo
Namespaces - Java Packages
namespace league {
class Hero {
};
}
@pati_gallardo
league::Hero h;
using namespace league;
Hero h;
Using namespace - Java Import static
@pati_gallardo
Using Namespace
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("Wonder Woman!");
cout << s;
}
@pati_gallardo
Values, Objects and Referring to them
@pati_gallardo
Anything Can Be a Value
int main()
{
Hero b;
int meaning = 42;
auto pi { 3.14 };
std::string s = "Hi!";
std::mutex my_mutex;
}
@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 : Hero & h
@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 : Hero * h
@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 : Hero h
* Sometimes the original value can be reused
@pati_gallardo
Range based For
vector<Hero> heroes;
for (auto & hero : heroes)
hero.rescue();
@pati_gallardo
Const : The Many Meanings class Hero {
public:
bool canFly() const;
void fly();
};
Hero superman;
deploy(superman);
void deploy(const Hero & h)
{
if (h.canFly()) // OK
h.fly(); //<-- ERROR
}
@pati_gallardo
const - Related to Final Variables in Java
Java
final Hero ptr; // ptr is final
C++
Hero * const ptr; // ptr is const
const Hero * ptr; // object is const
const Hero * const ptr; // object+ptr are const
@pati_gallardo
Immutable View Of An OBJECT
void crisis(const Hero & hero)
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 Hero & hero
Pass by reference : Hero & hero
Pass by value : Hero hero
Pass by pointer - be very careful
@pati_gallardo
Auto return type
auto meaning() { return 42; }
@pati_gallardo
Return by Value
Hero experiment() {
return Hero(); // return unnamed value
}
Hero experiment() {
Hero h;
return h; // return named value
}
@pati_gallardo
Return Value Optimization
Unnamed Return Value Optimization (RVO)
Named Return Value Optimization (NRVO)
@pati_gallardo
Slicing
Copying only parts of an object
class FlyingHero : public Hero {
int current_altitue = 0
}
FlyingHero superman;
Hero h = superman;
@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
Hero stackHero;
Heap
unique_ptr<Hero> heapHero =
make_unique<Hero>();
Hero * heapHero = new Hero();
@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;
{
unique_ptr<Hero> myHero =
make_unique<Hero>();
shared_ptr<Hero> ourHero =
make_shared<Hero>();
}
Smart Pointers
@pati_gallardo
Structure
@pati_gallardo
Class Declaration in Header File
Class Definition in Cpp File
Hero.h & Hero.cpp
A header is similar to an interface
Function declarations
Member variables
@pati_gallardo
Classes and Structs
@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
Hero heroFactory() {
static int num_created_heroes = 0;
++num_created_heroes;
return Hero();
} @pati_gallardo
Containers and Standard Types
@pati_gallardo
Use std::String - never char *
Use : std::string_view
Non-allocating, immutable string/substring
Note: Your project might have a custom string class
@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
@pati_gallardo
Segmentation fault (core dumped)
@pati_gallardo

Más contenido relacionado

La actualidad más candente

20130530-PEGjs
20130530-PEGjs20130530-PEGjs
20130530-PEGjs
zuqqhi 2
 
PHP traits, treat or threat?
PHP traits, treat or threat?PHP traits, treat or threat?
PHP traits, treat or threat?
Nick Belhomme
 

La actualidad más candente (20)

Using Jenkins for Continuous Integration of Perl components OSD2011
Using Jenkins for Continuous Integration of Perl components OSD2011 Using Jenkins for Continuous Integration of Perl components OSD2011
Using Jenkins for Continuous Integration of Perl components OSD2011
 
20130530-PEGjs
20130530-PEGjs20130530-PEGjs
20130530-PEGjs
 
Hachiojipm11
Hachiojipm11Hachiojipm11
Hachiojipm11
 
Trying to learn C# (NDC Oslo 2019)
Trying to learn C# (NDC Oslo 2019)Trying to learn C# (NDC Oslo 2019)
Trying to learn C# (NDC Oslo 2019)
 
Web2Day 2017 - Concilier DomainDriveDesign et API REST
Web2Day 2017 - Concilier DomainDriveDesign et API RESTWeb2Day 2017 - Concilier DomainDriveDesign et API REST
Web2Day 2017 - Concilier DomainDriveDesign et API REST
 
Construire son JDK en 10 étapes
Construire son JDK en 10 étapesConstruire son JDK en 10 étapes
Construire son JDK en 10 étapes
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.
 
A Lifecycle Of Code Under Test by Robert Fornal
A Lifecycle Of Code Under Test by Robert FornalA Lifecycle Of Code Under Test by Robert Fornal
A Lifecycle Of Code Under Test by Robert Fornal
 
PHP7: Hello World!
PHP7: Hello World!PHP7: Hello World!
PHP7: Hello World!
 
C++ The Principles of Most Surprise
C++ The Principles of Most SurpriseC++ The Principles of Most Surprise
C++ The Principles of Most Surprise
 
Incredible Machine with Pipelines and Generators
Incredible Machine with Pipelines and GeneratorsIncredible Machine with Pipelines and Generators
Incredible Machine with Pipelines and Generators
 
Make Your Own Perl with Moops
Make Your Own Perl with MoopsMake Your Own Perl with Moops
Make Your Own Perl with Moops
 
An introduction to ROP
An introduction to ROPAn introduction to ROP
An introduction to ROP
 
Mastering Namespaces in PHP
Mastering Namespaces in PHPMastering Namespaces in PHP
Mastering Namespaces in PHP
 
GraphQL API in Clojure
GraphQL API in ClojureGraphQL API in Clojure
GraphQL API in Clojure
 
PHP traits, treat or threat?
PHP traits, treat or threat?PHP traits, treat or threat?
PHP traits, treat or threat?
 
The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)
 
Smoking docker
Smoking dockerSmoking docker
Smoking docker
 
Secure Programming Practices in C++ (NDC Oslo 2018)
Secure Programming Practices in C++ (NDC Oslo 2018)Secure Programming Practices in C++ (NDC Oslo 2018)
Secure Programming Practices in C++ (NDC Oslo 2018)
 
Intro To Spring Python
Intro To Spring PythonIntro To Spring Python
Intro To Spring Python
 

Similar a C++ for Java Developers (SwedenCpp Meetup 2017)

Polyglot Programming in the JVM
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVM
Andres Almiray
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
frwebhelp
 
Hey, I need some help coding this C++ assignment.ObjectivesThis.pdf
Hey, I need some help coding this C++ assignment.ObjectivesThis.pdfHey, I need some help coding this C++ assignment.ObjectivesThis.pdf
Hey, I need some help coding this C++ assignment.ObjectivesThis.pdf
rishabjain5053
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
intelliyole
 

Similar a C++ for Java Developers (SwedenCpp Meetup 2017) (20)

C++ idioms.pptx
C++ idioms.pptxC++ idioms.pptx
C++ idioms.pptx
 
Anatomy of a Gradle plugin
Anatomy of a Gradle pluginAnatomy of a Gradle plugin
Anatomy of a Gradle plugin
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
Polyglot Programming in the JVM
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVM
 
C++ theory
C++ theoryC++ theory
C++ theory
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Dart for Java Developers
Dart for Java DevelopersDart for Java Developers
Dart for Java Developers
 
Pure functions and immutable objects @dev nexus 2021
Pure functions and immutable objects @dev nexus 2021Pure functions and immutable objects @dev nexus 2021
Pure functions and immutable objects @dev nexus 2021
 
Dart structured web apps
Dart   structured web appsDart   structured web apps
Dart structured web apps
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developers
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
 
Polyglot Programming in the JVM - Øredev
Polyglot Programming in the JVM - ØredevPolyglot Programming in the JVM - Øredev
Polyglot Programming in the JVM - Øredev
 
Dart, unicorns and rainbows
Dart, unicorns and rainbowsDart, unicorns and rainbows
Dart, unicorns and rainbows
 
C++ Interface Versioning
C++ Interface VersioningC++ Interface Versioning
C++ Interface Versioning
 
Polyglot Programming in the JVM - 33rd Degree
Polyglot Programming in the JVM - 33rd DegreePolyglot Programming in the JVM - 33rd Degree
Polyglot Programming in the JVM - 33rd Degree
 
Hey, I need some help coding this C++ assignment.ObjectivesThis.pdf
Hey, I need some help coding this C++ assignment.ObjectivesThis.pdfHey, I need some help coding this C++ assignment.ObjectivesThis.pdf
Hey, I need some help coding this C++ assignment.ObjectivesThis.pdf
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
 

Más de Patricia Aas

Más de Patricia Aas (20)

NDC TechTown 2023_ Return Oriented Programming an introduction.pdf
NDC TechTown 2023_ Return Oriented Programming an introduction.pdfNDC TechTown 2023_ Return Oriented Programming an introduction.pdf
NDC TechTown 2023_ Return Oriented Programming an introduction.pdf
 
Telling a story
Telling a storyTelling a story
Telling a story
 
Return Oriented Programming, an introduction
Return Oriented Programming, an introductionReturn Oriented Programming, an introduction
Return Oriented Programming, an introduction
 
I can't work like this (KDE Academy Keynote 2021)
I can't work like this (KDE Academy Keynote 2021)I can't work like this (KDE Academy Keynote 2021)
I can't work like this (KDE Academy Keynote 2021)
 
Dependency Management in C++ (NDC TechTown 2021)
Dependency Management in C++ (NDC TechTown 2021)Dependency Management in C++ (NDC TechTown 2021)
Dependency Management in C++ (NDC TechTown 2021)
 
Introduction to Memory Exploitation (Meeting C++ 2021)
Introduction to Memory Exploitation (Meeting C++ 2021)Introduction to Memory Exploitation (Meeting C++ 2021)
Introduction to Memory Exploitation (Meeting C++ 2021)
 
Classic Vulnerabilities (MUCplusplus2022).pdf
Classic Vulnerabilities (MUCplusplus2022).pdfClassic Vulnerabilities (MUCplusplus2022).pdf
Classic Vulnerabilities (MUCplusplus2022).pdf
 
Classic Vulnerabilities (ACCU Keynote 2022)
Classic Vulnerabilities (ACCU Keynote 2022)Classic Vulnerabilities (ACCU Keynote 2022)
Classic Vulnerabilities (ACCU Keynote 2022)
 
Introduction to Memory Exploitation (CppEurope 2021)
Introduction to Memory Exploitation (CppEurope 2021)Introduction to Memory Exploitation (CppEurope 2021)
Introduction to Memory Exploitation (CppEurope 2021)
 
Trying to build an Open Source browser in 2020
Trying to build an Open Source browser in 2020Trying to build an Open Source browser in 2020
Trying to build an Open Source browser in 2020
 
Trying to build an Open Source browser in 2020
Trying to build an Open Source browser in 2020Trying to build an Open Source browser in 2020
Trying to build an Open Source browser in 2020
 
DevSecOps for Developers, How To Start (ETC 2020)
DevSecOps for Developers, How To Start (ETC 2020)DevSecOps for Developers, How To Start (ETC 2020)
DevSecOps for Developers, How To Start (ETC 2020)
 
The Anatomy of an Exploit (NDC TechTown 2019)
The Anatomy of an Exploit (NDC TechTown 2019)The Anatomy of an Exploit (NDC TechTown 2019)
The Anatomy of an Exploit (NDC TechTown 2019)
 
Elections: Trust and Critical Infrastructure (NDC TechTown 2019)
Elections: Trust and Critical Infrastructure (NDC TechTown 2019)Elections: Trust and Critical Infrastructure (NDC TechTown 2019)
Elections: Trust and Critical Infrastructure (NDC TechTown 2019)
 
The Anatomy of an Exploit (NDC TechTown 2019))
The Anatomy of an Exploit (NDC TechTown 2019))The Anatomy of an Exploit (NDC TechTown 2019))
The Anatomy of an Exploit (NDC TechTown 2019))
 
Elections, Trust and Critical Infrastructure (NDC TechTown)
Elections, Trust and Critical Infrastructure (NDC TechTown)Elections, Trust and Critical Infrastructure (NDC TechTown)
Elections, Trust and Critical Infrastructure (NDC TechTown)
 
Survival Tips for Women in Tech (JavaZone 2019)
Survival Tips for Women in Tech (JavaZone 2019) Survival Tips for Women in Tech (JavaZone 2019)
Survival Tips for Women in Tech (JavaZone 2019)
 
Embedded Ethics (EuroBSDcon 2019)
Embedded Ethics (EuroBSDcon 2019)Embedded Ethics (EuroBSDcon 2019)
Embedded Ethics (EuroBSDcon 2019)
 
Chromium Sandbox on Linux (NDC Security 2019)
Chromium Sandbox on Linux (NDC Security 2019)Chromium Sandbox on Linux (NDC Security 2019)
Chromium Sandbox on Linux (NDC Security 2019)
 
Keynote: Deconstructing Privilege (C++ on Sea 2019)
Keynote: Deconstructing Privilege (C++ on Sea 2019)Keynote: Deconstructing Privilege (C++ on Sea 2019)
Keynote: Deconstructing Privilege (C++ on Sea 2019)
 

Último

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Último (20)

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 

C++ for Java Developers (SwedenCpp Meetup 2017)

  • 2. C++ for Java Developers @pati_gallardo Patricia Aas SweedenCpp Meetup C++ Stockholm 0x08 2017
  • 3. Patricia Aas - Vivaldi Browser Programmer - mainly in C++ Currently : Vivaldi Technologies Previously : Cisco Systems, Knowit, Opera Software Master in Computer Science - main language Java Twitter : @pati_gallardo
  • 4. Let’s Learn some Modern C++ (11-17) @pati_gallardo
  • 5. Don’t do This! Hero * h = new Hero(); EVER @pati_gallardo
  • 6. Your First Object #include "Hero.h" int main() { Hero h; } @pati_gallardo
  • 7. Your First Program #include <iostream> #include <string> int main() { std::string s("Wonder "); std::cout << s << "Woman!"; } @pati_gallardo
  • 8. Includes - Java Import Including your own headers #include "Hero.h" Including library headers #include <string> @pati_gallardo
  • 9. Namespaces - Java Packages namespace league { class Hero { }; } @pati_gallardo
  • 10. league::Hero h; using namespace league; Hero h; Using namespace - Java Import static @pati_gallardo
  • 11. Using Namespace #include <iostream> #include <string> using namespace std; int main() { string s("Wonder Woman!"); cout << s; } @pati_gallardo
  • 12. Values, Objects and Referring to them @pati_gallardo
  • 13. Anything Can Be a Value int main() { Hero b; int meaning = 42; auto pi { 3.14 }; std::string s = "Hi!"; std::mutex my_mutex; } @pati_gallardo
  • 14. 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 : Hero & h @pati_gallardo
  • 15. C++ Pointer * A C++ pointer is not a Java pointer It’s often called a “Raw Pointer” It’s a raw memory address UNSAFE : Hero * h @pati_gallardo
  • 16. 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 : Hero h * Sometimes the original value can be reused @pati_gallardo
  • 17. Range based For vector<Hero> heroes; for (auto & hero : heroes) hero.rescue(); @pati_gallardo
  • 18. Const : The Many Meanings class Hero { public: bool canFly() const; void fly(); }; Hero superman; deploy(superman); void deploy(const Hero & h) { if (h.canFly()) // OK h.fly(); //<-- ERROR } @pati_gallardo
  • 19. const - Related to Final Variables in Java Java final Hero ptr; // ptr is final C++ Hero * const ptr; // ptr is const const Hero * ptr; // object is const const Hero * const ptr; // object+ptr are const @pati_gallardo
  • 20. Immutable View Of An OBJECT void crisis(const Hero & hero) But the object is mutable - just not by you Mark functions that don’t mutate as const @pati_gallardo
  • 21. Parameter Passing, Return Values & Lambdas @pati_gallardo
  • 22. Parameter Passing Pass by const ref : const Hero & hero Pass by reference : Hero & hero Pass by value : Hero hero Pass by pointer - be very careful @pati_gallardo
  • 23. Auto return type auto meaning() { return 42; } @pati_gallardo
  • 24. Return by Value Hero experiment() { return Hero(); // return unnamed value } Hero experiment() { Hero h; return h; // return named value } @pati_gallardo
  • 25. Return Value Optimization Unnamed Return Value Optimization (RVO) Named Return Value Optimization (NRVO) @pati_gallardo
  • 26. Slicing Copying only parts of an object class FlyingHero : public Hero { int current_altitue = 0 } FlyingHero superman; Hero h = superman; @pati_gallardo
  • 27. Structured Bindings std::tuple<int, int> point(); auto [x, y] = point(); @pati_gallardo
  • 28. 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
  • 29. Memory : Lifetime & Ownership @pati_gallardo
  • 30. Where is it? Stack Hero stackHero; Heap unique_ptr<Hero> heapHero = make_unique<Hero>(); Hero * heapHero = new Hero(); @pati_gallardo
  • 31. Stack - Similar to “Try With Resources” Destroyed when exiting scope Deterministic Garbage Collection @pati_gallardo
  • 32. Loving the Stack #include <iostream> #include <string> using namespace std; int main() { { string s("Hello World!"); cout << s; } // <- GC happens here! } @pati_gallardo
  • 33. Hold a Value on the Stack that Controls The Lifetime of Your Heap Allocated Object using namespace std; { unique_ptr<Hero> myHero = make_unique<Hero>(); shared_ptr<Hero> ourHero = make_shared<Hero>(); } Smart Pointers @pati_gallardo
  • 35. Class Declaration in Header File Class Definition in Cpp File Hero.h & Hero.cpp A header is similar to an interface Function declarations Member variables @pati_gallardo
  • 37. 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
  • 38. 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
  • 39. All Classes Are Final by Default By extension : All methods are final by default @pati_gallardo
  • 40. Virtual Functions Pure virtual - Interface virtual void bootFinished() = 0; Use override void bootFinished() override; @pati_gallardo
  • 41. Multiple Inheritance is Allowed Turns out the ‘Diamond Problem’ is mostly academic ( and so is inheritance ;) ) @pati_gallardo
  • 42. All Classes Are “Abstract” Interface : only pure virtual methods Abstract Class : some pure virtual methods Plain Old Class : no pure virtual methods @pati_gallardo
  • 43. Structs A C++ Struct is a Class Where all members are public by default @pati_gallardo
  • 44. 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
  • 45. Static member / function in a Class Pretty much the same as Java @pati_gallardo
  • 46. Static Function in a Cpp File The function is local to the file @pati_gallardo
  • 47. Static Variable in a Function - Global variable - Only accessible in this function - Same variable across calls Hero heroFactory() { static int num_created_heroes = 0; ++num_created_heroes; return Hero(); } @pati_gallardo
  • 48. Containers and Standard Types @pati_gallardo
  • 49. Use std::String - never char * Use : std::string_view Non-allocating, immutable string/substring Note: Your project might have a custom string class @pati_gallardo
  • 50. Std::Vector Is Great std::vector has great performance Don’t use raw arrays Prefer std::vector or std::array @pati_gallardo
  • 51. 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