Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component.

Similar presentations


Presentation on theme: "C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component."— Presentation transcript:

1

2

3 C++

4 When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component.

5

6

7

8

9

10 C is pretty easy to learn but C++ is clearly complex. The basics are easy but it takes years to become a master in it (although C++11 is making it a bit easier).

11

12 very expert much ninja so impress wow!

13

14

15

16

17

18 void f() { int x{0}; }

19 void f() { int x{0}; int y{x}; // initialize y to be a copy of x y = x; // assign the value of x to y }

20 class MyClass { /*... */ }; void f() { MyClass x{}; MyClass y{x}; // initialize y to be a copy of x y = x; // assign the value of x to y }

21 void f() { int x{0}; // Construction int y{x}; // Copy Construction y = x; // Copy Assignment }// y.~int() // Destruction // x.~int()

22 class MyClass { /*... */ }; void f() { MyClass x; // Construction MyClass y{x}; // Copy Construction y = x; // Copy Assignment }// y.~MyClass() // Destruction // x.~MyClass()

23 class MyClass { public: MyClass() { puts("Constructing"); } ~MyClass() { puts("Destroying"); } MyClass(MyClass const&) { puts("Copying"); } void operator=(MyClass const&) { puts("Assigning"); } };

24 class MyClass { /* previously defined */ }; void f() { MyClass x; // Constructing MyClass y{x}; // Copying y = x; // Assigning }// y.~MyClass() // Destroying // x.~MyClass() // Destroying

25 class MyClass { /*... */ }; void f() { MyClass x; MyClass* p{&x}; // p is a pointer to x // *p is another name for x }

26 void f() { MyClass x; MyClass y; // Different objects have different addresses MyClass* px{&x}; // px points to x MyClass* py{&y}; // py points to y assert(px != py); }

27 void f() { MyClass* p{nullptr}; }

28 void f() { MyClass x; MyClass* px1{&x}; MyClass* px2{px1}; assert(px1 == px2); px1 = nullptr; assert(px1 != px2); }

29 // For value types, x and y are distinct int x = 0; int y = x; y = 42; // For reference types, x and y refer to the same object List x = new List (); List y = x; x.Add(1);

30 // x and *p denote the same object int x{0}; int* p{&x}; // Changes the value of x via *p *p = 42;

31 // x and *p denote the same object MyClass x{}; MyClass* p{&x}; // Changes the value of x via *p *p = get_thing();

32

33 void f() { MyClass x{}; // Construction MyClass y{x}; // Copy Construction y = x; // Copy Assignment }// y.~MyClass() // Destruction // x.~MyClass() // Destruction Lifetime of y Lifetime of x

34 MyClass x{}; // Global variable void f() { static MyClass y{}; // Function-local static variable }

35 void f() { MyClass* p{new MyClass{}}; delete p; } Lifetime of *p

36 MyClass* f() { MyClass* p{new MyClass{}}; return p; } void g() { MyClass* p{ f() }; delete p; }

37 void f() { int a = 0; // Copy initialization int b(0); // Direct initialization int c{0}; // Brace initialization int d{}; }

38 int global_int; // Zero-initialized void f() { int local_int; // Not initialized }

39 void f() { int local_int{}; MyClass local_MyClass{}; }

40

41

42 void f() { MyClass* p{new MyClass{}}; delete p; }

43 void f() { char* const buffer{new char[1024 * 1024]{}}; //...use the buffer... delete[] buffer; }

44 void f() { char* const buffer{new char[1024 * 1024]{}}; //... throw std::runtime_error{"oops“}; //... delete[] buffer; }

45 void f() { char* const buffer{new char[1024 * 1024]{}}; //... return; //... delete[] buffer; }

46 void f() { // Not actual C++ char* const buffer{new char[1024 * 1024]{}}; try { //...use the buffer... } finally { delete[] buffer; }

47 void f() { // Not actual C++ using (char* const buffer{new char[1024 * 1024]{}}) { //...use the buffer... }

48 void f() { scoped_buffer buffer{new char[1024 * 1024]{}}; //...use the buffer... }// buffer.~scoped_buffer()

49 struct scoped_buffer { public: scoped_buffer(char* const p) : _p{p} { } ~scoped_buffer() { delete[] _p; } char* get() const { return _p; } private: scoped_buffer(scoped_buffer const&) = delete; void operator=(scoped_buffer const&) = delete; char* _p; };

50 void f() { scoped_buffer buffer{new char[1024 * 1024]{}}; //...use the buffer... }// buffer.~scoped_buffer()

51 void f(size_t const buffer_size) { std::unique_ptr const buffer{new char[buffer_size]{}}; //...code that uses the buffer... }

52 std::unique_ptr f(size_t const buffer_size) { std::unique_ptr buffer{new char[buffer_size]{}}; return buffer; }

53

54 std::mutex _sync; void f() { _sync.lock(); //...synchronized code... _sync.unlock(); }

55 void f() { std::unique_lock lock(_sync); //...synchronized code... }

56 void f(char const* const file_name) { FILE* file(fopen(file_name, "r")); //...code that uses the file... fclose(file); }

57 void f(char const* const file_name) { std::ifstream file(file_name); //...code that uses the file... }

58

59

60

61

62 std::vector v{ 1, 2, 3, 4, 5, 6 }; for (size_t i{0}; i != v.size(); ++i) { std::cout << v[i] << '\n'; }

63 std::vector v{ 1, 2, 3, 4, 5, 6 }; for (auto it{v.begin()}; it != v.end(); ++it) // std::vector ::iterator { std::cout << *it << '\n'; }

64 std::set s{ 1, 2, 3, 4, 5, 6 }; for (auto it{s.begin()}; it != s.end(); ++it) // std::vector ::iterator not now! { std::cout << *it << '\n'; }

65 std::map m { { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 }, { 5, 10 }, { 6, 12 } }; for (auto it{m.begin()}; it != m.end(); ++it) { std::cout first second << '\n'; }

66 std::vector v{ 1, 2, 3, 4, 5, 6 }; for (auto it{v.begin()}; it != v.begin() + 3; ++it) { std::cout << *it << '\n'; }

67 std::vector v{ 1, 2, 3, 4, 5, 6 }; v.erase(v.begin() + 3); // Removes the 4 v.insert(v.begin() + 2, 10); // Inserts a 10 after the 2 int const x{*(v.begin() + 1)}; // Gets the 2 *(v.begin() + 1) = 9; // Changes the 2 to a 9 int const y{*(v.begin() + 1000)}; // Invalid!

68

69 Iterators ContainersAlgorithms

70 std::vector v{ 1, 3, 5, 2, 4, 6 }; std::sort(v.begin(), v.end());

71 std::vector v{ 1, 3, 5, 2, 4, 6 }; auto const it{std::find(v.begin(), v.end(), 2)}; if (it != v.end()) { std::cout << "I found a two!\n"; }

72 std::vector v{ 1, 2, 3, 4, 5, 6 }; if (std::binary_search(v.begin(), v.end(), 2)) { std::cout << "I found a two!\n"; }

73

74 std::vector v{ 1, 2, 3, 4, 5, 6 }; std::for_each(v.begin(), v.end(), [](int const x) { std::cout << x << '\n'; })

75 std::vector v{ 1, 2, 3, 4, 5, 6 }; std::transform(v.begin(), v.end(), v.begin(), [](int const x) { return x * x; });

76 std::vector v{ 1, 2, 3, 4, 5, 6 }; std::vector w(v.size()); std::transform(v.begin(), v.end(), w.begin(), [](int const x) { return x * x; });

77 std::vector v{ 1, 2, 3, 4, 5, 6 }; std::vector w; std::transform(v.begin(), v.end(), std::back_inserter(w), [](int const x) { return x * x; });

78 std::vector v{ 1, 3, 5, 2, 4, 6 }; std::sort(v.begin(), v.end(), [](int const lhs, int const rhs) { return lhs > rhs; });

79 std::vector v{ 1, 2, 3, 4, 5, 6 }; bool const all_are_even(std::all_of(v.begin(), v.end(), [](int const x) { return x % 2 == 0; }));

80 std::vector v{ 1, 2, 3, 4, 5, 6 }; std::set s(v.begin(), v.end());

81 int a[6]{ 1, 3, 5, 2, 4, 6 }; std::sort(begin(a), end(a));

82 void f(int* const data, size_t const count) { int* const first{data}; int* const last {data + count}; std::sort(first, last); }

83 template bool all_of(Iterator const first, Iterator const last, Function const f) { for (Iterator it{first}; it != last; ++it) { if (!f(*it)) return false; } return true; }

84

85 void f() { int x{0}; // x is an object int& rx{x}; // rx is a reference to x // it is another name for x }

86 void f(std::vector & v) { //...Use the vector... }

87 void f() { int const x{0}; x = 42; // Invalid; x is const }

88 void f() { int x(0); int const& rx(x); int const* px(&x); x = 42; // Ok, x is not const rx = 42; // Invalid; rx is const *px = 42; // Invalid; *px is const }

89 void f(std::vector const& v) { //...Use the vector but don't modify it... }

90 void f() { std::vector v = { 1, 2, 3, 4 }; for (const auto& x : v) { std::cout << x << '\n'; }

91 std::vector get_stuff(); void f() { auto the_stuff = get_stuff(); }

92 I was once on a project where performance was important, so the core of the program was written in C++. Other people would use it on different platforms including Unix and Linux.

93

94

95

96

97

98

99 I also use C++ when I need to access some specific features from CryptoAPI or create high performance stuff web server stuff. If you are planning to develop a math intensive library such as a quant library, stochastic analysis, signal processing or alike, go with C++.

100

101

102

103

104

105 I also use C++ when I need to access some specific features from CryptoAPI or create high performance stuff web server stuff. If you are planning to develop a math intensive library such as a quant library, stochastic analysis, signal processing or alike, go with C++.

106

107

108

109

110

111

112

113

114

115

116

117

118 www.microsoft.com/learning http://microsoft.com/msdn http://microsoft.com/technet http://channel9.msdn.com/Events/TechEd

119

120

121


Download ppt "C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component."

Similar presentations


Ads by Google