如何使用与函数同名的变量?
How do I use a variable that has the same name as a function?
我有给定的模板类,具有以下属性/类
template<class T1, class T2, int max>
class Collection{
T1 * _elementi1[max];
T2 * _elementi2[max];
int currently;
public:
Collection() {
for (size_t i = 0; i < max; i++) {
_elementi1[i] = nullptr;
_elementi2[i] = nullptr;
}
currently = 0;
}
~Collection() {
for (size_t i = 0; i < max; i++) {
delete _element1[i]; _element1[i] = nullptr;
delete _element2[i]; _element2[i] = nullptr;
}
}
T1 ** GetT1() { return _element1; }
T2 ** GetT2() { return _element2; }
int GetCurrent() { return currently; }
void Add(T1 t1, T2 t2) {
if (currently == max)
{
throw exception("MAX SIZE REACHED");
}
_element1[currently] = new T1(t1);
_element2[currently] = new T2(t2);
++currently;
}
friend ostream& operator<< (ostream &COUT, Collection&obj) {
for (size_t i = 0; i < obj.currently; i++)
COUT << *obj._element1[i] << " " << *obj._element2[i] << endl;
return COUT;
}
};
Max
用于限制集合的容量(我知道很愚蠢..(问题是我使用的#include <algorithm>
也有一个叫做max
的函数。每次我想使用变量智能感知和编译器时,都使用函数而不是变量。如何告诉编译器使用变量max
而不是函数?
在人们提交代码改进和其他建议之前也是如此。这是一个考试示例,其中不允许重命名/修改变量,您只能添加您认为合适的内容。
未经修改的"考试示例"是否编译?
是的,假设我不包括算法,它确实如此
我会说这是您在某处添加"使用命名空间 std;"的证据,也许是为了从算法<中获得一些功能>中获得一些功能>
如何告诉编译器使用变量 max 而不是 功能?
一种方法是取消编译器请求,将所有或任何命名空间 std 函数拉入本地命名空间......我的意思是删除"使用命名空间 std;">
现在,也许您需要<算法>的功能......如何在不同时拉入"std::max"的情况下获得它算法>
示例:从<算法>,我经常使用 shuffle((算法>
#include <algorithm>
// ... then I usually do
std::shuffle (m_iVec.begin(), m_iVec.end(), gen);
// I have no problem using the std:: prefix.
此时,函数 std::max(( 也被编译器知道,但不会与您的变量名称冲突。 访问该函数的唯一方法是通过"std::max(("符号。
还有另一种形式的"使用",如下所示:
#include <algorithm>
// now 'bring in' the feature you want.
using std::shuffle; // pull in shuffle, BUT NOT std::max
// ... so I now can do
shuffle (m_iVec.begin(), m_iVec.end(), gen);
// and have no worries about max being interpreted as a function
max = 0;
永远之后,我必须搜索我在这里调用的"shuffle(("方法的提示/提醒。
相关文章:
- EXPECT_CALL中变量的使用
- 变量在使用赋值语句赋值后恢复为以前的值
- 在非 constexpr 函数中作为左值传递的变量上使用 'constexpr' 函数
- 当设置为 const 变量时使用 nullptr
- 通知条件变量后使用互斥锁
- 成员变量如何使用专用类模板?
- 在未初始化的变量上使用复合赋值运算符(+=, ..)不是C++中的UB?
- 文本 0 和 0 作为变量如何使用函数 __builtin_clz 产生不同的行为?
- 为什么在同一条件变量上使用多个互斥锁会使此代码崩溃?
- 为什么原子不能与自动变量一起使用
- 在C 中的非阵列变量上使用delete []
- QString变量在使用QT Creator从键盘输入的双变量后跳过
- C 中的多线程程序:在标志变量上使用Mutex
- 成员变量在使用前重置
- 如何使用静态变量,使用自定义Getter和设置器在SWIG中扩展结构
- C 初始化一个变量并使用std :: cin分配
- terminateThRead()在线程上使用CloseHandle(),仅使用堆栈普通变量(不使用planc)会泄漏内
- 模板类静态成员变量在使用'extern template class'语法时的特殊化
- 我需要创建一个函数在用户下打印"=",但由于变量是使用 main() 声明的,因此函数看不到参数
- 有什么方法可以将字符串文字u8与变量一起使用吗