简体   繁体   中英

Variable name after struct definition

What are bar and x below and why are they located there? Are they instances of the struct s?

I was reading this Stackoverflow question whose code I have pasted here and I found that the poster and one of the answers use variable names [ bar and x ] after the definition of the struct . cpp reference doesn't use this syntax their examples and I don't know what the syntax is called nor what the syntax does to look it up.

If it is just about creating an instance of a struct , why not write it in the next line like cpp reference does in its examples?

struct _bar_ 
    {
        int a;
    } bar; 
struct test {
      int value;
   } x;

struct test { int value; } x; declares x to be an object of type struct test . It is a simple declaration, the same as int x , with struct test { int value; } struct test { int value; } in place of int .

In

struct _bar_ {
    int a;
} bar; 

bar is a declaration of a variable of type struct _bar_ .

It's the same as:

struct _bar_ bar;

By using this definition:

struct _bar_ 
    {
        int a;
    } bar; 

Here you've just declared a variable of type struct _bar_ .

struct product {
  int weight;
  double price;
} ;

product apple;
product banana, melon;

In this case, where object_names are specified, the type name (product) becomes optional: struct requires either a type_name or at least one name in object_names, but not necessarily both.

From: Data structures

As all other answers have stated, defining a name after a struct definition creates an instance of that struct with that name in whatever scope it was defined. So, struct __bar__ { ... } bar; is equivalent to struct __bar__ { ... }; __bar__ bar; struct __bar__ { ... }; __bar__ bar; . As of writing, however, none of the other answers attempt to answer the second question: "Why not write it in the next line like cpp reference does in its examples?"

The most practical argument I can think of for using this "name after" syntax is when you only need one instance of this struct. This way, it is guaranteed that both the definition and declaration happen in the same place; which is not guaranteed using the "next line" approach. This also has the added benefit of only having one place where the struct is defined, which means if you want to change it at any point you only have to go to 1 spot instead of 2 (potentially disjunct) ones.

This translates well for when you (absolutely) need a single set of data in a global scope:

// 1 bar, in the global scope
struct Bar {
    int a;
} global_bar;

int main()
{
    // Do something with global bar
    global_bar.a = 100;

    return 0;
}

Whereas, if you wanted multiples of the struct you would probably tend towards code like:

struct Bar {
    int a;
};

int main()
{
    // 2 bars, in the local scope
    Bar bar_1 = Bar();
    Bar bar_2 = Bar();

    // Do something with the local bars...

    return 0;
}

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