English 中文(简体)
A. 组合结构变化问题
原标题:Problem with const array changing c++

我有这一守则试图保护用户免受阵列边界错误的影响。

因此,我拿不出为什么会汇编,th一ve就宣布阵列会合起来,一,,以找到一个汇编错误!

非常感谢。

/************ file: SafeAccessArray.h ********************/
template<typename T>
class SafeAccessArray
{
private:
int _len;
T * _arr;
public:
SafeAccessArray (int len=2) : _len (len), _arr (new T [len]) {}
~SafeAccessArray () { delete _arr; }
T& operator [](int i) const
{if (i < 0 || i >= _len) throw (-1);
else return _arr[i]; }
};
/************ end of file: SafeAccessArray.h *************/

/************ file: SafeAccessArray1.cpp *************/
#include "SafeAccessArray.h"
int main()`enter code here`
{
SafeAccessArray<int> intArr (2);
intArr[0] = 0;
intArr[1] = 1;
const SafeAccessArray<int> intArrConst (2); // THIS IS THE "PROBLEMATIC" LINE
intArrConst [0] = 0;
intArrConst [1] = 1;
return 0;
}
/************ end of file: SafeAccessArray1.cpp ******/
问题回答

缩略语 T&营运人[](int i) const noway。 你回去了参考,这一功能可以被提上主角。

加入<条码> T&。 更糟糕的是,停止。 Just use std:vector and the at ( function.

我认为,<代码>operator[]个成员职能希望以下两种超载变量:

T& operator [](int i);
const T& operator [](int i) const;

提供

T& operator [](int i) const;

这与上述任何内容不相称,因此问题也不相同。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签