可以在结构中的函数? [英] Can functions be in a struct?

查看:79
本文介绍了可以在结构中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以在struct中描述函数吗?例如,此代码是否有效?

 结构函数{
int func(){return 5; }
}; /en.wikipedia.org/wiki/C%2B%2B_classes\"> struct 之间的唯一差异 在C ++中是:


在C ++中,结构是一个使用struct关键字定义的类。它的成员和基类默认是公共的。默认情况下,使用class关键字定义的类具有私有成员和基类。这是C ++中结构和类之间的唯一区别。



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

struct 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++.

这篇关于可以在结构中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆