49

I was pretty confused about the difference between struct and class as I seemed to see them used for pretty much the same things. I googled the differences and the only answer I saw was that structs have public members by default and classes have private members by default. However, my lecturers have just told me that structs cannot contain member functions. But I have seen many threads on the internet where people include member functions in structs and specifically say that it is alright to do so.

My lecturers seem adamant that structs by definition cannot have functions, so what is going on? The only thing I could think of is that maybe the compiler changes functions within a struct to something else so that they technically don't contain functions... Is there a clear answer to these contradictions?

sion
  • 1,367
  • 6
  • 17
  • 21
  • 9
    Maybe they mean C. – chris Jun 13 '14 at 02:42
  • 39
    If this is supposed to be a C++ course, drop it immediately. – aschepler Jun 13 '14 at 02:44
  • 3
    Point your lecturers to this document. https://isocpp.org/files/papers/N3797.pdf -- The working draft of the C++ standard, chapter 9. The words class and struct are used mostly interchangeably. Paragraph 3 even has an example of a struct with a member function. – Benjamin Lindley Jun 13 '14 at 02:48
  • 3
    the only real difference is that by default struct inheritance is public (as it is default member access), whereas class is private. Other than that, they are EXACTLY the same. @aschepler, spot on! – vsoftco Jun 13 '14 at 03:30

5 Answers5

50

I googled the differences and the only answer I saw was that structs have public members by default and classes have private members by default.

Yes, this is correct. In addition, bases of a struct are inherited publicly by default, whereas bases of a class are inherited privately by default.

Declaring a function as a member of a struct has precisely the same semantics as declaring a function as a member of a class, except for the difference you've noted. In each case they are called member functions.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
7

C++ structs can definitely have member functions. C structs, on the other hand, are a much different beast -- they're essentially arrays that provide names and type information for certain indicies.

Community
  • 1
  • 1
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
4

In the C++98 standard:

A structure is a class defined with the class-key struct; its members and base classes (clause 10) are public by default (clause 11).

and

Members of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or union are public by default.

So it means that the only difference between struct and class is the default member access control that is public or private.

Can C++ struct have member functions?

Yes, they can.

My lecturers seem adamant that structs by definition cannot have functions, so what is going on?

If it is a lecture on C, it is correct. If it is a lecture on C++, it is not correct.

The only thing I could think of is that maybe the compiler changes functions within a struct to something else so that they technically don't contain functions... Is there a clear answer to these contradictions?

Yes, there is a clear answer: C++ struct can have member functions.

YuGiOhJCJ
  • 343
  • 2
  • 7
2

It seems to me that just because something is allowed by a language does NOT mean it is a good idea. Although technically a C++ struct can contain functions, I don't think the original intent of the struct data type was to contain functions. Was it not the main purpose of introducing a Class type to support defining the data and the logic that acts on the data in a close knit relationship, thus promoting healthier interfaces to the data?

Travis
  • 552
  • 1
  • 8
  • 14
  • 3
    This is demonstrably false. Stroustrop himself has said as much on numerous occasions. A struct and a class are the same except as mentioned in the other answers. – jonspaceharper Jul 21 '16 at 16:19
  • 1
    So why add Classes to the language at all then? Why NOT just use structs.... I've never been a big fan of C++ simply because it seems overly complex...to many ways to shoot yourself in the foot. Having two nearly identical keywords in the language seems like a bad idea...certainly newer languages have chosen NOT to do that. Python tends to offer one and only one obviously correct way to do things and that lends itself to more maintainable code. Perhaps Stroustrop did make them nearly identical on purpose...if so, I say, "shame on him for confusing me" :) – Travis Jul 22 '16 at 23:49
-2

I am currently working on a project, and I just realized that my struct needs to be able to provide a comparison operators in order for the sorted list that contains it to be able to do the sorting. So it makes sense to have operator overloading in a struct. That's the only legitimate reason to have a struct with methods.