Skip to content

Instantly share code, notes, and snippets.

@FranklinChen
Created April 9, 2014 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FranklinChen/10288187 to your computer and use it in GitHub Desktop.
Save FranklinChen/10288187 to your computer and use it in GitHub Desktop.
// Illustrate disciplined C++ code
// http://www.cplusplus.com/reference/vector/vector/
#include <vector>
#include <iostream>
using namespace std;
class Foo {
public:
virtual ~Foo() {
cout << "destroying foo" << endl;
}
};
auto main() -> int {
try {
// Prevent memory leak.
const auto foo = make_shared<Foo>();
const vector<int> v { 3, 1, 4 };
// Note that v[i] does not do bounds checking, but v.at(i) does.
// http://www.cplusplus.com/reference/vector/vector/at/
for (int i = 0; i < 4; i++) {
const auto x = v.at(i);
cout << "v at " << i << " is " << x << endl;
}
}
catch(const exception &e) {
cout << "caught exception: "<< e.what() << endl;
}
}
@FranklinChen
Copy link
Author

Output:

v at 0 is 3
v at 1 is 1
v at 2 is 4
destroying foo
caught exception: vector

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment