-5

When I define some random structure like for example in a cpp file in Visual studio

1)    struct CAddition {
    int x, y;

    CAddition(int a, int b) { x = a; y = b; }
    int result() { return x + y; }
};

and now if I define some structure object

CAddition foo;

it works without any error but if I use any alias in the end

2) struct CAddition {

    int x, y;

CAddition(int a, int b) { x = a; y = b; }
int result() { return x + y; }
}CAddition;

I cannot simply define any object without using struct before the definition

 struct CAddition foo;

or an alternative method would be to add

typedef struct CAddition { 

In the method 2 to avoid rewriting struct every time , My question is whats the difference between these 2 definitions , doesn't method 1 use less keywords and much easier to use in what conditions should we use the second definition of a structure.

Novice_Developer
  • 1,432
  • 2
  • 19
  • 33
  • 1
    In 2) you declare a variable named `CAddition` of the type `CAddition` you just created, thus you hide the type, and you need `struct` to tell the compiler you want the type and not the object, though I don't know if this is actually allowed by the standard... – Holt Apr 30 '18 at 09:47
  • 1
    `struct CAddition { /*..*/}CAddition;` defines both a type and an instance (with same name), it is not an alias. – Jarod42 Apr 30 '18 at 09:47
  • 1
    Possible duplicate of [Difference between 'struct' and 'typedef struct' in C++?](https://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c) –  Apr 30 '18 at 09:48
  • Think of it this way: a `struct` definition in C++ is merely a `class` with public members only, unless specified otherwise. It doesn't behave differently – Fluffy Apr 30 '18 at 09:51

2 Answers2

5

With struct CAddition { ... } CAddition; you are doing two things:

  1. You define the structure CAddition as a typename. That's what the struct CAddition thing does.
  2. You define the variable CAddition. The variable is the one after the structure.

Because you define the variable CAddition you can not use that name for the type, as then the compiler will think you mean the variable and not the structure. To solve this you need to use struct CAddition to explicitly tell the compiler you mean the structure typename.


On an unrelated note: A struct is just like a class, with the difference being that all members a public by default in a struct. So you don't need the public specification in a struct.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

it works without any error but if I use any alias in the end

In 2) you are not using an "alias", but you create an instance of that class you just defined (structs and classes are the same in c++ btw). You then have an instance with a name that shadows the class name and as a consequence you need the struct CAddtion to tell the compiler that CAddition refers to the class not to the instance you created.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185