相关疑难解决方法(0)

不支持在c或c ++中添加两个指针.为什么?

为什么在c或c ++中不支持添加两个指针.

当我做,

int *ptr,*ptr1;
int sum = ptr + ptr1;
Run Code Online (Sandbox Code Playgroud)

C或C++会抛出错误.虽然它支持,

int diff = ptr - ptr1;
Run Code Online (Sandbox Code Playgroud)

c c++ pointers

16
推荐指数
1
解决办法
2万
查看次数

为什么我不能添加指针?

我的代码非常类似于此:

LINT_rep::Iterator::difference_type LINT_rep::Iterator::operator+(const Iterator& right)const
{
    return (this + &right);//IN THIS PLACE I'M GETTING AN ERROR
}

LINT_rep::Iterator::difference_type LINT_rep::Iterator::operator-(const Iterator& right)const
{//substracts one iterator from another
    return (this - &right);//HERE EVERYTHING IS FINE
}

err msg: Error  1   error C2110: '+' : cannot add two pointers
Run Code Online (Sandbox Code Playgroud)

为什么我只在一个地方而不是两个地方都收到错误?

c++ pointers

13
推荐指数
2
解决办法
2万
查看次数

标签 统计

c++ ×2

pointers ×2

c ×1