Telegram Group & Telegram Channel
✔️ ЗАДАЧА: Что выведет код?



#include <iostream>
#include <vector>

struct Tracer {
Tracer(const char* name) : name(name) {
std::cout << "Constructing " << name << "\n";
}
~Tracer() {
std::cout << "Destructing " << name << "\n";
}
const char* name;
};

struct Example {
Tracer t1{"t1"};
std::vector<Tracer> list;
Tracer t2{"t2"};

Example() : list{Tracer("temp1"), Tracer("temp2")} {
std::cout << "Inside constructor\n";
}
};

int main() {
Example e;
std::cout << "End of main\n";
return 0;
}


---

ОТВЕТ:

Constructing t1
Constructing temp1
Constructing temp2
Inside constructor
Constructing t2
End of main
Destructing t2
Destructing temp2
Destructing temp1
Destructing t1

---

Почему так:

• Поля инициализируются в порядке объявления в структуре, а не в списке инициализации
• std::initializer_list создаёт временные объекты, которые копируются в вектор
• t2 создаётся после строки Inside constructor
• Деструкторы вызываются в обратном порядке: t2 → temp2 → temp1 → t1

Хитрость — в порядке инициализации, временных объектах и destructuring-порядке!


@cpluspluc
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/cpluspluc/1037
Create:
Last Update:

✔️ ЗАДАЧА: Что выведет код?



#include <iostream>
#include <vector>

struct Tracer {
Tracer(const char* name) : name(name) {
std::cout << "Constructing " << name << "\n";
}
~Tracer() {
std::cout << "Destructing " << name << "\n";
}
const char* name;
};

struct Example {
Tracer t1{"t1"};
std::vector<Tracer> list;
Tracer t2{"t2"};

Example() : list{Tracer("temp1"), Tracer("temp2")} {
std::cout << "Inside constructor\n";
}
};

int main() {
Example e;
std::cout << "End of main\n";
return 0;
}


---

ОТВЕТ:

Constructing t1
Constructing temp1
Constructing temp2
Inside constructor
Constructing t2
End of main
Destructing t2
Destructing temp2
Destructing temp1
Destructing t1

---

Почему так:

• Поля инициализируются в порядке объявления в структуре, а не в списке инициализации
• std::initializer_list создаёт временные объекты, которые копируются в вектор
• t2 создаётся после строки Inside constructor
• Деструкторы вызываются в обратном порядке: t2 → temp2 → temp1 → t1

Хитрость — в порядке инициализации, временных объектах и destructuring-порядке!


@cpluspluc

BY C++ Academy


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/cpluspluc/1037

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

Traders also expressed uncertainty about the situation with China Evergrande, as the indebted property company has not provided clarification about a key interest payment.In economic news, the Commerce Department reported an unexpected increase in U.S. new home sales in August.Crude oil prices climbed Friday and front-month WTI oil futures contracts saw gains for a fifth straight week amid tighter supplies. West Texas Intermediate Crude oil futures for November rose $0.68 or 0.9 percent at 73.98 a barrel. WTI Crude futures gained 2.8 percent for the week.

How Does Telegram Make Money?

Telegram is a free app and runs on donations. According to a blog on the telegram: We believe in fast and secure messaging that is also 100% free. Pavel Durov, who shares our vision, supplied Telegram with a generous donation, so we have quite enough money for the time being. If Telegram runs out, we will introduce non-essential paid options to support the infrastructure and finance developer salaries. But making profits will never be an end-goal for Telegram.

telegram from sg


Telegram C++ Academy
FROM USA