开发者

Disable class pointer increment/decrement operators

For example code:

A* pA = new A;

I need to avoid pointer increment/decrement operators during compilation phase:

pA++; // MUST failed d开发者_C百科uring compilation phase


You could declare pA as

A * const pA = new A;

This makes pA a const pointer to an A object. The pointer cannot be changed after initialisation, but the contents of it can.

compare

const A *pA = new A;

which is a pointer to a const A object.

If you want to iterate over an array of A objects get a separate pointer.

A * const pAs = new A[size];

for (A * iter = pAs; iter < pAs+size; ++iter)
{
    // do stuff
}


That's impossible to do.

Since I guess you want to do that to avoid unintended errors, I guess smart(ass) solutions do not apply (including inventing some pointer-like wrapper classes etc) because they will increase the probability of errors :)


If you want to stick with raw pointers, it's impossible.

You need to wrap it with a class that doesn't implement that operators (aka smart pointers).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜