开发者

Can functions be in a struct?

开发者 https://www.devze.com 2023-01-10 00:13 出处:网络
Can we describe functions in a struct? For example, is this code valid? str开发者_StackOverflow中文版uct function {

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号