gpt4 book ai didi

c++ - 重载运算符的返回值

转载 作者:太空狗 更新时间:2023-10-29 23:28:12 24 4
gpt4 key购买 nike

如果这是一个非常基本的问题,我很抱歉,我是 C++ 的新手。我正在尝试为它定义自己的 vector 类和迭代器。但是,每当我重载运算符时,返回的值始终是一个地址。

例如,当我想要打印 1 0 时,下面的代码打印 0x7fb6dbc000e0 0x7fb6dbc000e0

由于我已经研究了一段时间的语法,一些运算符看起来有点不同,这只是为了让您看到我尝试过的一些东西。

测试.cc

#include <iostream>
#include "TwoWayVector.cc"
int main(){
TwoWayVector<int> numbers;
numbers.push_back(3);
numbers.push_back(2);
TwoWayVectorIterator<int>* beginning = numbers.begin();
TwoWayVectorIterator<int>* beginning2 = numbers.begin();
cout << beginning==beginning2;
cout << beginning != beginning2;
cout << endl;
return 0;
}

TwoWayVector.cc

using namespace std;
#include "TwoWayVectorIterator.cc"
template <class T> class TwoWayVector{
public:

T* data;
int capacity;
int nextFree;

TwoWayVector(){
capacity = 10;
nextFree = 0;
data = new T[capacity];
}

~TwoWayVector(){
delete data;
}

T& operator[](const int index){
if( index >= capacity || capacity + index < 0){
string number = static_cast<ostringstream*>( &(ostringstream() << index) )->str();
string error = "index " + number + " is out of bounds";
throw error;
}
else if(index < 0){
return data[nextFree+index];
}
return data[index];
}
bool operator==(const TwoWayVector* vector2){
if(capacity != vector2->capacity){
return false;
}
if(nextFree != vector2->nextFree){
return false;
}
for(int i=0; i<nextFree ; i++){
if(data[i] != vector2[i]){
return false;
}
}
return true;
}
//memory leaks?
void push_back(T object){
if(capacity <= nextFree){
capacity = capacity*2;
T* tmp = new T[capacity];
for(int i=0; i<capacity; i++){
tmp[i] = data[i];
}
delete data;
data = tmp;
}
data[nextFree] = object;
nextFree++;
}

T pop_back(){
nextFree--;
T result = data[nextFree];
data[nextFree] = NULL;
return result;
}

int size(){
return nextFree;
}

TwoWayVectorIterator<T>* begin(){
TwoWayVectorIterator<T>* i = new TwoWayVectorIterator<T>(0,this);
return (i);
}
TwoWayVectorIterator<T>* end(){
TwoWayVectorIterator<T>* i = new TwoWayVectorIterator<T>(nextFree,this);
return(i);
}

};

TwoWayVectorIterator.cc

template<typename T> class TwoWayVector;

template <class T> class TwoWayVectorIterator{
public:
TwoWayVector<T>* vector;
int currentPosition;
TwoWayVectorIterator(TwoWayVector<T>& vec){
currentPosition = 0;
vector = vec;
}
TwoWayVectorIterator( int pos , TwoWayVector<T>* vec){
currentPosition = pos;
vector = vec;
}

bool operator==(const TwoWayVectorIterator* vector2){
bool contents, position;
contents = (vector == vector2) ? true : false;
cout << contents << endl;
position =(currentPosition == vector2->currentPosition) ? true : false;
return (contents && position);
}

bool& operator!=(const TwoWayVectorIterator* vector2){
bool contents, position;
contents = (vector == vector2) ? false : true;
position=(currentPosition == vector2->currentPosition) ? false : true;
return (contents || position);
}

TwoWayVectorIterator& operator++(){
return *this;
currentPosition = (currentPosition+1);

}
TwoWayVectorIterator& operator++(int){
currentPosition = (currentPosition+1);
return *this;
}
TwoWayVectorIterator& operator=(TwoWayVectorIterator* vector2){
&vector = vector2;
currentPosition = vector2->currentPosition;
return *this;
}
TwoWayVectorIterator& operator+(int n){
currentPosition = currentPosition+n;
return *this;
}
TwoWayVectorIterator& operator-(int n){
currentPosition = currentPosition-n;
return *this;
}
bool& operator<(TwoWayVectorIterator* vector2){
return (currentPosition<vector2->currentPosition);
}
T& operator*(){
return vector[currentPosition];
}
};

最佳答案

cout << beginning==beginning2;

意味着

cout << (beginning==beginning2);

确实意味着

(cout << beginning) == beginning2;

http://en.cppreference.com/w/cpp/language/operator_precedence

因此您正在打印 TwoWayVectorIterator<int>* , 不是 bool .

关于c++ - 重载运算符的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15952603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com