سؤال

What's the behavior of sizeof used in member initializer? I can't find a description about exactly that. In the below code example is it safe? ie, after struct member will the compiler put right size of A in b?

struct A
{
    int a = 0;
    int b = sizeof(A);
    int c = 3;
    int d = 4;
    char s[256];

    A()
    {
        //printf("b = %d\n", b);
    }
};
هل كانت مفيدة؟

المحلول

The size of a struct is determined much earlier in the compilation than the setting of the values inside the struct.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top