env PKG_CONFIG_PATH=/usr/x86_64-w64-mingw32/lib/pkgconfig \
../configure \
--extra-ldflags="-static-libstdc++ -static-libgcc" \
--enable-static \
--disable-shared \
--enable-nonfree \
--enable-libfaac \
--enable-libfdk-aac \
--enable-cross-compile \
--pkg-config=pkg-config \
--arch=x86_64 \
--target-os=mingw32 \
--cross-prefix=x86_64-w64-mingw32-
friend and Encapsulation
Some people believe that the idea of having friend classes violates the principle of encapsulation because it means that one class can get at the internals of another. One way to think about this, however, is that friend is simply part of a class's overall interface that it shows the world. Just like an elevator repairman has access to a different interface than an elevator rider, some classes or functions require expanded access to the internals of another class. Moreover, using friend allows a class to present a more restrictive interface to the outside world by hiding more details than may be needed by anything but the friends of the class.
Finally, friends are particularly common in cases of operator overloading because it is often necessary for an overloaded operator to have access to the internals of the classes that are arguments to the operator.
Typical use cases of friend functions are operations that are conducted between two different classes accessing private or protected members of both.
#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::seconds(1));
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::this_thread::sleep_for(std::chrono::microseconds(500));
#include <iostream>
#include <iomanip>
using namespace std;
struct Point
{
int x;
int y;
};
inline bool is_valid(const Point& p)
{
bool result = true;
// some checks
return result;
}
struct Rect : Point
{
int width;
int height;
};
inline bool is_valid(const Rect& r)
{
bool result = is_valid(static_cast<const Point&>(r)) && r.width > 0 && r.height > 0;
return result;
}
int main() {
// теряется возможность делать так:
//Rect r = {0, 1, 2, 3};
Rect r{};
r.x = 0;
r.y = 1;
r.width = 10;
r.height = 11;
auto check = is_valid(r);
cout << " x=" << r.x
<< " y=" << r.y
<< " w=" << r.width
<< " h=" << r.height
<< " valid=" << check
<< endl;
cout << sizeof(Rect) << "/" << sizeof(int)*4 << endl;
uint8_t *ptr = reinterpret_cast<uint8_t*>(&r);
auto beg = ptr;
auto end = ptr + sizeof(Rect);
for (auto it = beg; it != end; ++it) {
cout << hex << setfill('0') << setw(2) << (int)*it << " ";
}
cout << endl;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
struct Point
{
int x;
int y;
bool is_valid() const
{
bool result = true;
// some checks
return result;
}
};
struct Rect : Point
{
int width;
int height;
bool is_valid() const
{
bool result = Point::is_valid() && width > 0 && height > 0;
return result;
}
};
void pass_point(const Point &p)
{
cout << "point is valid: " << p.is_valid() << endl;
}
int main() {
// теряется возможность делать так:
//Rect r = {0, 1, 2, 3};
Rect r{};
r.x = 0;
r.y = 1;
r.width = 0; // make invalid
r.height = 11;
auto check = r.is_valid();
cout << " x=" << r.x
<< " y=" << r.y
<< " w=" << r.width
<< " h=" << r.height
<< " valid=" << check
<< endl;
// но следующий вызов скажет что точка валидна:
// вызов валиден, т.к. Rect отнаследован от Point
pass_point(r);
cout << sizeof(Rect) << "/" << sizeof(int)*4 << endl;
uint8_t *ptr = reinterpret_cast<uint8_t*>(&r);
auto beg = ptr;
auto end = ptr + sizeof(Rect);
for (auto it = beg; it != end; ++it) {
cout << hex << setfill('0') << setw(2) << (int)*it << " ";
}
cout << endl;
return 0;
}
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <deque>
#include <map>
#include <thread>
#include <mutex>
std::mutex lock;
void pipeline(const std::string&, std::vector<std::string>&);
std::string files[4] =
{
"file1.txt",
"file2.txt",
"file3.txt",
"file4.txt"
};
int main()
{
std::string string;
std::vector<std::string> str;
std::map<std::string, std::size_t> freq;
std::deque<std::thread> pool;
std::ofstream out("out.txt");
for (std::size_t i = 0; i < 3; i++)
{
pool.push_back(std::thread(pipeline, std::ref(files[i]), std::ref(str)));
}
while (pool.size())
{
pool.front().join();
pool.pop_front();
}
std::cout << str.size();
return 0;
}
void pipeline(const std::string &infile, std::vector<std::string> &str)
{
std::ifstream in(infile.c_str());
while (!in.eof())
{
std::string temp;
std::getline(in, temp);
{
std::unique_lock<std::mutex> guard(lock);
str.push_back(temp);
}
}
}
std::locale system("");
std::locale::global(system);
#include <iostream>
#include <locale>
using namespace std;
int main()
{
std::locale system("");
std::locale::global(system);
wstring str = L"hello, world. привет, МКС";
wcout << str << endl;
wcout << str.c_str() << endl;
wcout << 3.14 << endl;
// А теперь будет десятичный разделитель согласно локали выводится
wcout.imbue(system);
wcout << 3.14 << endl;
return 0;
}
hello, world. привет, МКС
hello, world. привет, МКС
3.14
3,14
while (true) {
std::getline(std::cin, line);
if (!std::cin)
break;
std::cout << "out: " + line << std::endl;
}
while (std::cin) {
std::getline(std::cin, line);
std::cout << "out: " + line << std::endl;
}
extern "C" int printf(const char *format, ...);