tags:

views:

212

answers:

5

Can we describe functions in a struct? For example, is this code valid?

struct function {
  int func() { return 5; }
};
A: 

Yes, all of the current compilers support it. Some basic struct use

And the only difference default visibility

DumbCoder
All C++ compilers support it, and always have.
anon
@Neil worded it as mentioned.
DumbCoder
+4  A: 

Yes, the only differences between a struct and class in C++ are:

In C++, a structure is a class defined with the struct keyword. Its members and base classes are public by default. A class defined with the class keyword has private members and base classes by default. This is the only difference between structs and classes in C++.

Justin Ethier
A: 

Yes, it is totally part of the standard.

A struct is a class with all its members public by default.

Alexandre Abreu
A: 

Yes, you can, the difference to class is the access limitations to its members and methods. The struct has all its members and methods public by default.

Juan Macek
+5  A: 

Yes. There is no functional difference between a class and a struct, and anything you can do with one (such as defining a member function), you can do with the other.

If you're interested, the only difference is that members and base classes are private by default in a class, and public by default in a struct.

Mike Seymour