-2

I have known that structs in C doesn't support functions / constructors like a class in C++ . However I did try putting in function definitions and constructors and the code behaved as if I have defined a class and not struct . I used visual studio 2010 for my code . Is this a standard feature or just that it works only with MS C++. I searched many forums and they had mixed responses .

Prashant
  • 91
  • 3
  • 8

3 Answers3

1

In C++ Structs and Classes are the same except for one thing. A class' members and methods are private be default, a Struct's are public by default.

Boumbles
  • 2,473
  • 3
  • 24
  • 42
  • 1
    And `struct` inheritance is public by default. And the `struct` keyword cannot be used in template parameter declarations. – Robᵩ Mar 12 '13 at 02:11
1

struct and class are functionally the same in C++ except members in a struct are public by default and in a class are private by default. in fact this previous thread covers it in itty bitty details.

If you compiled successfully than you must have been using C++.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • yeah thats right , I did use C++ , but was confused about the behaviour . I thought the C part in C++ should behave as it is. But does it also mean that it could be replacement for the classes in C++ . Like achieve everything from it as we would have by using a class – Prashant Mar 12 '13 at 02:10
  • @Prashant C++ behaves differently from C in too many ways to describe here, you can use `struct` instead of `class` but most people will only use `struct` for very simple objects. Please read the link I provided it has some great commentary on when to use either one. – Shafik Yaghmour Mar 12 '13 at 02:16
1

This is a standard feature of C++ but not of C. You must have been compiling in C++.

QuentinUK
  • 2,997
  • 21
  • 20