С++ для интервью
37 subscribers
10 photos
Готовься к собеседованиям по C++ эффективно: реальные вопросы, типичные HR уловки, примеры кода и объяснения.
Каждый пост — один шаг ближе к офферу 💼
Download Telegram
🎯 auto и const: подводный камень

const int x = 10;
auto a = x;
auto& b = x;


Что здесь происходит?


a — копия, const отброшен

b — ссылка на const int

💡 auto отбрасывает const и ссылку, если они не указаны явно
Please open Telegram to view this post
VIEW IN TELEGRAM
Какой тип у b?
Anonymous Quiz
43%
int&
57%
const int&
0%
int
0%
const int
👍4
😉 Move ≠ Copy

std::string a = "hello";
std::string b = std::move(a);


Многие думают, что a очищается. Это не совсем так...
Please open Telegram to view this post
VIEW IN TELEGRAM
👍4
🔍 RAII и исключения

void f() {
std::lock_guard<std::mutex> g(m);
throw std::runtime_error("error");
}
Please open Telegram to view this post
VIEW IN TELEGRAM
🌃 virtual деструктор

struct Base {
~Base() = default;
};

struct Derived : Base {
~Derived() { std::cout << "dtor\n"; }
};

Base* b = new Derived;
delete b;
Please open Telegram to view this post
VIEW IN TELEGRAM
💻 std::vector и инвалидирование

std::vector<int> v = {1,2,3};
auto it = v.begin();
v.push_back(4);
Please open Telegram to view this post
VIEW IN TELEGRAM
👍4
❔ CRTP vs virtual

CRTP (Curiously Recurring Template Pattern) — идиома проектирования, при которой шаблон класса принимает производный класс в качестве параметра шаблона

template<typename D>
struct Base
{
void call() { static_cast<D*>(this)->impl(); }
};
Please open Telegram to view this post
VIEW IN TELEGRAM
👍4
🤯 Работа с auto
int x = 10;
auto f() { return (x); }
Please open Telegram to view this post
VIEW IN TELEGRAM
Что вернёт f()?
Anonymous Quiz
80%
int
0%
int&
20%
const int&
👍4
🔍 Есть ли ошибка в коде ниже?
std::string_view f() {
return std::string("hello");
}
Please open Telegram to view this post
VIEW IN TELEGRAM
std::unique_ptr и move
std::unique_ptr<int> a = std::make_unique<int>(5);
auto b = a;
🍿 Как поведет себя функция?
int x = 10;
auto f = [=]() mutable { ++x; };
f();
Please open Telegram to view this post
VIEW IN TELEGRAM