简体   繁体   中英

C++ constructor initialization list alternative in C?

In C++, classes constructors can use initialization lists, which I am told is a performance feature that improves by avoiding extra assignments. So I wonder if there is a similar approach to achieve the same benefits in C for functions that basically serve the same purpose to initialize structs as C++ class constructors?

I am a little unclear on how exactly the feature works in a C++ compiler, so any additional info on the subject will also be appreciated.

C doesn't have any similar feature, however since C also doesn't have constructors, there is no danger of unnecessary assignments.

The bigger principle is that introducing one feature into a language often creates a need for additional features to reinforce the original. An trivial example is threads. If threads are built into the language as a feature, then there is the immediate question of how to synchronize them. Hence synchronization is also needed. So you see languages (like C) with no built-in threads or synchronization and languages with both, but not one without the other. Here, constructors is to threads as synchonization is to list initializers.

In C++ constructors, initialization lists allow the C++ compiler to constructor members in-place, at the location of the member variable, instead of using an assignment operator, copy-constructor, or move-constructor to initialize the member variable. See Section 10.6 of the C++ FAQ for more details.

In C, there are no such automatic operations provided by the C compiler. This means that the programmer controls all initialization directly, and no special-language features are required to avoid these extra operations.

To be a little more clear, consider what happens when you use assignment to initialize in a C++ constructor:

  1. The member variable is first constructed with a default constructor
  2. A temporary object is constructed
  3. An assignment or move-assignment operator is called to re-initialize the member variable with the temporary.
  4. Call the destructor on the temporary.

While some compilers can optimize this away in some situations, your mileage may vary, and no C++ compiler can optimize these steps away in all situations. Now, consider how a programmer would exactly duplicate these steps in C:

void my_struct_init(struct my_struct* sp)
{
  member_init_default(&sp->the_member);  /* default constructor for member */

  struct member memb; /* temporary on stack */
  member_init_other(&memb, ...params...);  /* initialize memb */
  member_assign(&sp->the_member,&memb);    /* assign member */
  member_finalize(&memb);                  /* finalize the temporary */
}

Few C programmers would do this (without good reason). Instead, they would automatically code the optimization:

  member_init_other(&sp->the_member, ...params...);

The feature exists in C++ because the compiler does a lot of automatic things for the programmer. This is often makes life easier for the programmer, but requires features like initialization lists to help the compiler generate optimum code. C compilers present a much simpler model of the underlying machine, do fewer things automatically, and thus require fewer features (though not necessarily less work) to generate similarly optimal code.

在C语言中没有这样的功能。最接近的是初始化器

You can write a separate function and call it when you are creating an object from a class and then pass it to it:

C:

typedef struct {
        int x;
}mine;

void mine_initializer(mine* me)
{
        me->x = 4; //initialization
}

int main(void)
{
        mine me;
        mine_initializer(&me);
        return 0;
}

Also you can do that in C++:

struct mine{
        int x;
        void initialize()
        {
                x = 4; //initialization
        }
};



void main(void)
{
        mine me;
        me.initialize();
        printf("%d",me.x);
}

this will output 4 as the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM