C++面向对象程序中的获取和设置方法
Get and Set Methods in C++ OOP
在我的计算机科学类中,头文件中有一个枚举值,而get和set函数有问题。我对C++还很陌生。这是我头文件的一部分:
enum class SchoolType {
Elementary = 1,
Secondary = 2
};
class School : public Institution
{
public:
// There are a couple more values here
SchoolType GetSchoolType();
void SetSchoolType(SchoolType typeSchool);
};
这是我.cpp文件的一部分:
SchoolType GetTypeSchool()
{
return this->_typeSchool;
}
void SetTypeSchool(SchoolType typeSchool)
{
}
但是"this"会带来一个错误,并表示"this"只能在非静态成员函数中使用。如何使该函数工作?我的计算机老师告诉我,这就是我应该如何编码get函数,但我仍然不明白,是不是我在标题中做错了什么?
对于.cpp文件,应该有:
SchoolType School::GetSchoolType() {
return this->_typeSchool;
}
void School::SetSchoolType(SchoolType typeSchool) {
// Insert code here...
}
基本上,在C++中,当定义成员函数时,您需要指定函数属于哪个类(在本例中为School
),否则编译器不会将其视为任何类的一部分。您还需要保持方法名称的一致性(GetSchoolType
与GetTypeSchool
)。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- Poco 库 PUT 方法未按预期工作,尽管主机、方法、内容类型设置正确
- 如何在Visual Studio 2017上将类方法设置为参数并将它们与lambda一起使用?
- 将类方法设置为 OpenSSL 回调
- 哪个是设置位的最佳方法以及为什么?
- 设置窗口几何图形特性的正确方法
- 有没有一种方法可以在不设置精度的情况下设置小数位数
- 只设置一次方法中变量的值
- MySQL c++ 连接器:使用 SetBlob() 在查询中设置多个 blob 数据的正确方法是什么?
- 将函数更改为具有设置函数输入变量的方法的类
- 谷歌基准自定义设置和拆卸方法
- 如何定义和设置指向模板类方法的功能指针
- 如何在签名sa_sigaction中设置类方法?
- C++自动吸气器设置方法的类模板 - 好/坏的做法?
- 我如何调用我的3个数据成员中每个人的设置方法,然后显示由我的设置方法设置的值
- 当包含具有虚拟方法的标头时,不确定如何在.cpp文件中设置方法
- 设置方法C++时出错
- C++面向对象程序中的获取和设置方法
- 没有调用googletest设置方法
- 数组添加和设置方法