The document provides an overview of key differences between Java and C++ for Java developers learning C++. It covers topics like includes and namespaces for importing code, value types versus references, parameter passing, const, classes and structs, static, containers and standard library types. Code examples are provided to illustrate concepts like object creation, references, pointers, lambda expressions, memory management using smart pointers, and using standard containers and algorithms.
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
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
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
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
26. Slicing
Copying only parts of an object
class FlyingHero : public Hero {
int current_altitue = 0
}
FlyingHero superman;
Hero h = superman;
@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
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
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
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