Skip to content

Commit

Permalink
Add const to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelprotas committed Dec 22, 2024
1 parent cce0daf commit b6076e8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions homework/vector-of-shared-ptrs/vectorFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

#include <iostream>

std::vector<std::shared_ptr<int>> generate(int count) {
std::vector<std::shared_ptr<int>> generate(const int count) {
std::vector<std::shared_ptr<int>> result = {};
for (int i = 0; i < count; i++) {
std::shared_ptr<int> ptr = std::make_shared<int>(i);
auto ptr = std::make_shared<int>(i);
result.push_back(ptr);
}
return result;
}

void print(const std::vector<std::shared_ptr<int>> vector) {
for (auto ptr : vector) {
void print(const std::vector<std::shared_ptr<int>>& vector) {
for (const auto& ptr : vector) {
std::cout << *ptr << std::endl;
}
std::cout << std::endl;
}

void add10(std::vector<std::shared_ptr<int>> vector) {
for (auto ptr : vector) {
void add10(const std::vector<std::shared_ptr<int>>& vector) {
for (const auto& ptr : vector) {
if (ptr) {
*ptr += 10;
}
Expand All @@ -32,8 +32,8 @@ void sub10(int* const ptr) {
}
}

void sub10(std::vector<std::shared_ptr<int>> vector) {
for (auto ptr : vector) {
void sub10(const std::vector<std::shared_ptr<int>>& vector) {
for (const auto& ptr : vector) {
sub10(ptr.get());
}
}
8 changes: 4 additions & 4 deletions homework/vector-of-shared-ptrs/vectorFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <vector>
#include <memory>

std::vector<std::shared_ptr<int>> generate(int count);
void print(std::vector<std::shared_ptr<int>> vector);
void add10(std::vector<std::shared_ptr<int>> vector);
std::vector<std::shared_ptr<int>> generate(const int count);
void print(const std::vector<std::shared_ptr<int>>& vector);
void add10(const std::vector<std::shared_ptr<int>>& vector);
void sub10(int* const ptr);
void sub10(std::vector<std::shared_ptr<int>> vector);
void sub10(const std::vector<std::shared_ptr<int>>& vector);

0 comments on commit b6076e8

Please sign in to comment.