rubixibuc
rubixibuc

Reputation: 7397

Structs and Classes in C++

In c++ if you create a struct or class like this

struct foo {

}

Must you use the struct or class qualifier to create another one as you do in c, if not why is that? When would it be appropriate to use it in C++

Example

struct foo a;

Thanks :-)

Upvotes: 1

Views: 130

Answers (4)

cyco130
cyco130

Reputation: 4934

You can, but you don't have to. And you better not, it's the proper C++ style if you don't intend to mix your code with pure C. The reason is that the designers of C++ thought that C's concept of tags was unnecessarily complicated. I tend to agree.

Upvotes: 2

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

The struct is not required. In C++, the only difference between a struct and a class is that in a struct, members are public by default, while in a class, they're private by default.

Upvotes: 3

Foo Bah
Foo Bah

Reputation: 26261

In C you can avoid it by using a typedef. In C++ the type is created automatically hence no need to explicitly state it.

The C syntax to make it similar:

typedef struct {
....
} foo;

Then you can use it by saying foo x;

Upvotes: 1

Mahesh
Mahesh

Reputation: 34625

No, struct keyword is not required while instantiating in C++. By the way, struct definition should end with a ;

Upvotes: 3

Related Questions