4

Can we use access specifiers - private and protected - in C++ structs (as opposed to classes)?

Also, do access modifiers exist in C?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Priya_fsr
  • 91
  • 1
  • 1
  • 6
  • 7
    This is a very elementary question, answerable by a quick check of any C++ reference or tutorial. – Keith Thompson Mar 25 '13 at 14:47
  • 2
    [What are the differences between struct and class in C++](http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c) – Bo Persson Mar 25 '13 at 14:51
  • 1
    [Is 'private' a C keyword?](http://stackoverflow.com/questions/623219/is-private-a-c-keyword?rq=1) – Bo Persson Mar 25 '13 at 14:59
  • 1
    Re-reading the question, it's not as elementary as I thought (I misread it the first time). The answer isn't obvious if you don't know the relationship between structs and classes. (And there's a style issue of whether you *should* use access specifiers in a struct, though that's not what you asked.) If you'll edit the question, I'll withdraw my downvote. – Keith Thompson Mar 25 '13 at 17:07

6 Answers6

13

C doesn't have C++style access modifiers. A C struct is just a composite object type containing members of other object types.

In C++, a struct and a class are almost identical; the only difference is that struct members are public by default, and class members are private by default. So this:

struct foo {
private:
    // ...
};

is equivalent to this:

class foo: {
    // ...
};

This has been answered elsewhere.

This implies that the private, public, and protected keywords are equally valid in either a struct definition or a class definition.

As a matter of programming style, on the other hand, if you're going to be using access modifiers, it's probably best to define your type as a class rather than as a struct. Opinions will differ on this, but IMHO the struct keyword should be used for POD (Plain Old Data) types, or for types that could be defined as structs in C.

C++ structs, strictly speaking, are very different from C structs, and are nearly identical to C++ classes. But if I see something defined in C++ as a struct, I expect (or at least prefer) it to be something similar to a C struct.

Community
  • 1
  • 1
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
3

In C++ a structure is the same as class with the only difference that the default scope is public unlike private that is the default scope for class. In C access specifiers don't exist but after all what would you use them for?

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
2

Yes, you can use public, protected in private in C++ structures.

No, the access modifiers don't exist in C.

In C++, the only difference between class and struct is that the members of a class are by default private, whereas the members of a struct are by default public. This means means that a C++ struct can have member functions, constructors, overloaded operator and use any other features of a class.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

struct is not very different from class in c++. The default visibility is public instead of private. C does not support those.

Thibaut
  • 2,400
  • 1
  • 16
  • 28
0

The only difference between a struct and a class in C++ is that a struct's members are public by default and a class's members are private by default. You can use access specifiers in both of them, just as you can use anything else in both of them.

There are no access specifiers in C.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

Can we use access specifiers - private and protected - in struct of c++?

Yes. A struct is a class; the only difference is the default accessibility (public for struct, and private for class) if you don't specify it.

Also is the use of access modifier allowed in C language also? Does access specifiers really exist in C??

No, C does not have access specifiers.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644