Using C style in modern C ++ class

I have a class like:

Fragment 1:

struct B
{
    int member;
    // more complex members e.g. arrays of structs etc
};

This class is used in code, assuming it is a C style structure (e.g. used in memcpy , memset , etc.)

As part of good programming principles, I consider modification B as follows:

Fragment 2

struct B 
{
    B() {}
    int member;
    // more complex members e.g. arrays of structs etc
};

Here is my understanding. Please correct if I am wrong a. Fragment 1 defines B, which is POD, while Snippet 2 defines B, which is not POD, AND b. In Snippet 2, B can still be legally used for C-style use like memset and memcpy

  • std :: is_pod: false
  • std :: is_trivial: false
  • std :: is_trivially_copyable: true
  • std :: is_trivial: false

, Big 3 (, , Big 5), , .

3:

struct B
{
    B() {}
    ~B(){}
    B& operator=(B &){ return *this; }
    B(B const &r){}
    int member;

    // more complex members e.g. arrays of structs etc
};

. , , . Snippet 3 B C, memset memcpy

  • std:: is_pod: false
  • std:: is_trivial: false
  • std:: is_trivially_copyable: false
  • std:: is_trivial: false

B Snippet 2 Snippet 3 C. .

C is_trivially_copyable is_trivial ( )?

$3.9/2 , ++ 11 "is_trivially_copyable" . ++ (++ , , ) , POD vs non POD, , ++ 11 .

Petes, , ,

+4
1

POD , , , . . ++ 11 .

struct B
{
    int member = 0; // this will initialize the data member to 0
};

, int, member .

struct B
{
    int member;
    explicit B(int member = 0) : member(member) {}
};

- (9/6) (9/7), , C.


, , .

struct B 
{
    B() {}
    int member;
    // more complex members e.g. arrays of structs etc
};

, , . , default.

struct B 
{
    B() = default;
    int member;
    // more complex members e.g. arrays of structs etc
};

, C.

+2

Source: https://habr.com/ru/post/1543717/


All Articles