gpt4 book ai didi

c++ - C++ 结构的函数

转载 作者:IT老高 更新时间:2023-10-28 12:07:41 27 4
gpt4 key购买 nike

通常我们可以为 C++ 结构定义一个变量,如

struct foo {
int bar;
};

我们也可以为结构定义函数吗?我们将如何使用这些功能?

最佳答案

是的,除了默认访问级别(成员方式和继承方式)之外,structclass 相同。 (以及与模板一起使用时 class 的额外含义)

一个类支持的每个功能因此都由一个结构支持。您使用的方法与用于类的方法相同。

struct foo {
int bar;
foo() : bar(3) {} //look, a constructor
int getBar()
{
return bar;
}
};

foo f;
int y = f.getBar(); // y is 3

关于c++ - C++ 结构的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13125944/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com