C++ For Java Programmers: 15-494 Cognitive Robotics Ethan Tira-Thompson
C++ For Java Programmers: 15-494 Cognitive Robotics Ethan Tira-Thompson
Programmers
15-494 Cognitive Robotics
Ethan Tira-Thompson
Keyword Mapping
• Java ➙ C++
• Java API ➙ STL (& friends, e.g. Boost)
• Generics ➙ Templates
• Same syntax:
Vector<foo> ➙ vector<foo>
#include <iostream>
class Foo {
public:
Foo.h: void sayHello() {
std::cout << "Hello World!" << std::endl;
}
}; you are going to hate this
6
C++ File Layout
• Do you want everything in the .h?
• Might want to improve readability, or avoid inline
• Split definition from implementation
#include <iostream> System Header
Foo.h:
class Foo {
public:
void sayHello();
};
void Foo::sayHello() {
std::cout << "Hello World!" << std::endl;
}
Scope Specification 7
Template Usage
• Allows a class to be re-used with a
variety of types
• Canonical example: vector
• Want to store a resizable array of data, but it doesn’t
really matter what the data is
• vector<T>, where T is any type: vector<int>,
vector<string>, vector<Foo>, etc.
• A class may make assumptions about type/capabilities
of its template argument — results in cryptic
compiler errors when the wrong type is passed.
8
Template Usage
n e ed
’t
Code: ow w e d o
r
n
y w h e re Output:
N eve
#include <vector> ::
std
0
1
using namespace std; 2
3
int main (int argc, char * const argv[]) { 4
5
6
vector<int> v; 7
for(int i=0; i<10; ++i) 8
9
v.push_back(i); //each call stores a copy of i The 0th element is: 0
The 1th element is: 1
//iterator usage The 2th element is: 2
for(vector<int>::iterator it=v.begin(); it!=v.end(); ++it) The 3th element is: 3
The 4th element is: 4
cout << *it << endl; The 5th element is: 5
The 6th element is: 6
//indexed usage The 7th element is: 7
The 8th element is: 8
for(int i=0; i<v.size(); ++i) The 9th element is: 9
cout << "The " << i << "th element is: " << v[i] << endl;
return 0;
a tor
} p e r
r o g!
y fo d in
a
or erlo a
o
H ov 9
C++ Memory Management
• For every new, there should be a delete
• Arrays have to use ‘delete []’
int * a=new int[10];
/* ... */
delete [] a;
10
Pointers vs. References
• When you have a pointer, prepend ‘*’ to
access the value pointed to, and use ‘->’
instead of ‘.’ to access members.
• References always return the referenced
value
• Can’t reassign a reference, must define at creation
char c = 'a';
char * p = &c; // p holds the memory address of c
char c2 = *p; // c2 == 'a'
*p = 'b'; // c == 'b' , c2 unaffected
p = &c2; // p now holds the address of c2: *p == c2 == 'a', c==’b’
References
int & pi; // illegal (uninitialized reference)
char&& ppc; // illegal (no reference to reference)
int& ap[15]; // illegal (can't create array of references)
int& f(char&); // legal! Function taking a char&, returns a reference to int
char c = 'a';
char & p = c; // p now references c
char c2 = p; // c2 == 'a'
p = 'b'; // c == 'b' , c2 unaffected
p = c2; // c == 'a' , p still references c: p == c == c2 == 'a'
12
Pointers vs. References
class Foo {
public:
int member;
string getName() const { return "Foo"; }
};
Foo a;
Foo & r = a; //reference
Foo * p = &a; //pointer
// good
int f(int x);
// good
void setValues(vector<int>& v);
16
Value vs. Pointer vs. Reference
• Don’t forget ‘const’!
• Get in the habit of using const by default, removing
it when necessary
• Say you are creating a function ‘muck’ which is not
supposed to modify the value it is passed
void muck(Foo f); //bad, how big is 'Foo'?
void muck(Foo& f); //bad, might accidentally modify original
void muck(const Foo& f); //good - no copy, and still read-only
17
Value vs. Pointer vs. Reference
• Thus, the ideal function definitions:
// we’ll assume f doesn’t intend to return a value in x
int f(int x);
// last one...
void setValues(const vector<int>& v);
18
Value vs. Pointer vs. Reference
• Don’t return references to local variables
Shape& unify(const Shape& s1, const Shape& s2) {
Shape ans;
ans = /* however union is done... */
return ans; // BZZZT, error:
// ans is disappearing, how can it be referenced?
}
19
Gotchas: Slicing
• Slicing
• Store pointers when inheritance may be possible
• can’t store references, no way to reassign them
25