Download presentation
Presentation is loading. Please wait.
1
Workshop 1++: A bit of C++
Wouter van Toll Path Planning course September 17, 2015
2
Workshop 1++: A bit of C++
C++ at first sight Industry standard May feel old-fashioned Separate header files Not much is built in (e.g. visualization, geometry) Manage memory yourself (to some extent) Easy to make mistakes Powerful if you know what you’re doing Could be more efficient than other languages Improvements Smarter compilers Standard libraries (STL): std::vector, std::map, iterators I’ll name a few differences to e.g. C# / Java October 22, 2019 Workshop 1++: A bit of C++
3
Stack vs. heap October 22, 2019
4
Stack vs. heap Two ways to create/manage objects in memory
Stack: Thing t(1,2); Creates a Thing. t is destroyed when out of scope Heap: Thing* t = new Thing(1,2); Creates a Thing. t points to its memory address Pointer can go out of scope; object stays in memory You need to delete the object yourself at some point Main pro: efficiency, control Objects can share pointers Pointers can be set to nullptr Main con: memory management Undeleted objects: Pointers to deleted objects memory leaks October 22, 2019 Workshop 1B: C++
5
Pointers and references
Pointers to stack objects Thing t(1,2); Thing* ptr = &t; ptr->doSomething(); Or you can use references Thing t(1,2); Thing& ref = t; ref.doSomething(); References to heap objects Thing* t = new Thing(1,2); Thing& ref = *t; Thing ref = *t; Some differences Pointers can be null, references cannot std::vector<Thing*> is allowed, std:: vector<Thing&> is not * = pointer &t = “memory address of t” & = reference *t = “the Thing to which t points” Creates a copy of t! October 22, 2019 Workshop 1B: C++
6
const and functions Objects are copied unless you say otherwise*
Let’s say class X stores a Point Point X::getLocation() { return this->p; } Point& X::getLocation() { ... } const Point& X::getLocation() { ... } const Point& X::getLocation() const { ... } Point& X::getLocation() const { ... } Point p = x.getLocation(); Point& p = x.getLocation(); const Point& p = x.getLocation(); Point p( x.getLocation() ); const Point& p = average(p1,p2); const Point p = average(p1,p2); Point& p = average(p1,p2); Returns a copy of the point Returns a mutable reference Returns a non-mutable reference Promises that X won’t change Compile error (why?) Copies the point (again?)* OK? Depends on the function No copy; p cannot be changed No copy; p can be changed Possibly equivalent nowadays Compile error/warning (why?) October 22, 2019 Workshop 1B: C++
7
const and parameters Pass object parameters as references to prevent copying void computeNewVelocity(Character& dude) { ... } const determines which parameters can change Point average(const Point& a, const Point& b) { ... } Non-const is useful for C-style “out parameters” void average(const Point& a, const Point& b, Point& result) { ... } Sometimes more efficient, but often less intuitive I only use out-parameters if I need multiple return values I use const a lot Makes you think about what does what. No side effects. Many languages don’t have it (why?) Many students don’t like it (why?) You could hack your way around it October 22, 2019 Workshop 1B: C++
8
The final slide Questions? Remarks? October 22, 2019 Workshop 1B: C++
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.