Struct Definition

In the tutorial on Structs it says the following:

1
2
3
4
5
6
struct product {
  int weight;
  double price;
} apple, 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.



If the type name is optional, i.e. excluded, then how is the type defined? If I were to write the code as such:

1
2
3
4
struct {
  int weight;
  double price;
} apple, banana, melon;


then how would I, or anyone reading the code, know that this is a struct of type product?
Last edited on
how would I, or anyone reading the code, know that this is a struct of type product?
There is no type "product" here. apple, banana and lemon are variables of unnamed type.
Before C++11 you could not create any additional variables past this point (in C++11 there are auto and decltype)
Topic archived. No new replies allowed.