将成员功能实现为外部功能
Implementing a member function as an external function
我刚刚开始我的高级数据结构类,在处理类基础时,我们的问题之一是"将成员函数max()实现为外部函数。"
我已经浏览了C++入门、C编程语言和我们的文本,但都只提到了外部变量声明,并谈到了它作为某种全局快捷方式的使用。
有人能帮我弄清楚如何在外部实现我的会员功能,并帮助我理解提出这个问题的书背后的逻辑吗?
我的代码:
democlass.h
#ifndef Assignment_1_democlass_h
#define Assignment_1_democlass_h
class demoClass
{
public:
demoClass(int a = 5, int b = 10);
int max() const;
int getA() const; //my added code, not in provided text example
int getB() const; //my added code, not in provided text example
private:
int itemA, itemB;
};
#endif
main.cpp
#include "democlass.h"
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char * argv[])
{
demoClass obj1(7,9);
demoClass obj2(12);
demoClass obj3;
cout << obj1.getA() << " " << obj1.getB() << endl;
cout << obj2.getA() << " " << obj2.getB() << endl;
cout << obj3.getA() << " " << obj3.getB() << endl;
cout << endl;
cout << obj2.max() << endl << obj3.max();
return 0;
}
demoClass::demoClass(int a, int b){
itemA = a;
itemB = b;
}
int demoClass::max() const{
if (itemA > itemB)
return itemA;
else
return itemB;
};
int demoClass::getA() const{
return itemA;
};
int demoClass::getB() const{
return itemB;
};
下面的代码能工作吗?
inline int max(const demoClass& demo)
{
return demo.max();
}
相关文章:
- C++:函数外部的超时功能
- NASM 调用外部C++功能
- C |声明类外部功能
- 当用作模板参数时,功能指针是否需要指向具有外部链接的函数
- 是否有一种方法可以让公共成员在班级外部无法解码,而无需访问包装器功能
- 如何在我的指针和阵列功能中修复未解决的外部错误-LNK2019和LNK1120
- 使用JNI访问外部本机功能
- 在外部功能中具有一个变量初始化或内部功能中的多个初始化是最有效的
- 错误LNK2019:未解决的外部符号-MSVC调试构建功能模板
- C 常数和骨料内部功能主体与外部
- 类功能中未解决的外部符号
- 如何在C 中调用外部模板功能
- 如何从窗口功能外部访问wm_command
- 主外部功能的C++ "unresolved external symbol"
- 从班级外部超载成员功能
- C++对 C 语言以外的语言的外部"C"功能
- 使用无功能的外部"C"
- 当它是指向类功能的指针时,如何正确调用函数,但在该类外部
- 当与可能导致缓冲区溢出的功能一起使用时,外部变量是否比其他变量更大
- 类静态功能外部链接