Can functions be in a struct?
Can we describe functions in a struct? For example, is this code valid?
str开发者_StackOverflow中文版uct function {
int func() { return 5; }
};
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++.
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
.
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.
Yes, it is totally part of the standard.
A struct is a class with all its members public by default.
精彩评论