POD etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
POD etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

15 Kasım 2017 Çarşamba

POD

POD Parametreli Constructor Sunabilir
Bu bir POD tipidir.
struct Object
{
  int i;
  double d;
  Object(int ii, double dd) : i(ii), d(dd) {}
};
POD ve Kopyalama
Açıklaması şöyle. Sadece alanlar kopyalanır. Padding byte'ları kopyalanmaz.
The implicitly-defined copy/move constructor for a non-union class X performs a memberwise copy/move of its bases and members.
Örnek
Elimde bir POD olsun
struct A {
    char a;
    int b;
};
Kopyalama yapalım. Sadece alanlar kopyalanır. Padding byte'ları kopyalanmaz.
A x = ...;
A y =x;
İlla herşey kopyalansı istersek şöyle yaparız.
union A{ struct {
  char a;
  int b;
}; };
POD Aggreate Initialization Yapabilir
Şöyle yaparız. Daha detaylı bilgi için Brace Initialization - Aggregate yazısına bakılabilir.
class A
{
public:
  int x = 0, y = 0;
};
..
A a{1,2}; // using the sequence constructor created by the compiler, great
POD Constructor Exception Fırlatmaz
POD constructor'ları exception fırlatamaz.
int main()
{
  int i;    //will here throw exception?
}
POD ve memcpy
POD tipler memcpy ile kullanılabilir. Şöyle yaparız.
Object clone (Object o1)
{
   char buf [sizeof(Object)];
   memcpy(buf, &o1, sizeof(dest));
   Object* ptr = reinterpret_cast<Object*>(dest);
   return *ptr;
}
POD ve memcmp
POD'larda memcmp kullanmak aslında çok iyi bir fikir değil. Çünkü kopyalama yaparken padding alanları kopyalanmayabilir. Açıklaması şöyle
memcmp() between two objects of type struct{char c; int n;} will compare the padding bytes whose values may differ when the values of c and n are the same
Elimde bir POD olsun
struct A {
    char a;
    int b;
};
Kopyalama yapalım. Bu durumda memcmp() doğru çalışmayabilir.
A x = ...;
A y =x;
POD ve Socket
Elimizde POD olsun.
struct outgoing_header
{
  std::int32_t a;
  std::int64_t b;
  char c[SIZE};
};
Göndermek için şöyle yaparız.
outgoing_header oh{...};
send(&oh, sizeof(oh));
Okumak için şöyle yaparız.
constexpr static size_t max_size = ...;
char buf[max_size]{};

size_t got_bytes = receive(&buf, max_size);

// now we need to interpret these bytes as outgoing_header
outgoing_header* pheader = reinterpret_cast<outgoing_header*>(&buf[0]);

// now access the header items
pheader->a;
pheader->b;