重载运算符的返回值

时间:2013-04-11 15:18:46

标签: c++ templates pointers reference iterator

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

例如,当我希望打印0x7fb6dbc000e0 0x7fb6dbc000e0时,以下代码会打印1 0

由于我一直在搞乱语法,有些操作符看起来有点不同,这只是为了让你看到我尝试过的一些东西。

test.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];
}
};

2 个答案:

答案 0 :(得分:9)

cout << beginning==beginning2;

意味着

cout << (beginning==beginning2);

确实意味着

(cout << beginning) == beginning2;

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

因此,您打印的是TwoWayVectorIterator<int>*,而不是bool

答案 1 :(得分:5)

&lt;&lt;&lt;&lt;&lt;运算符高于==和!=运算符的precedence。所以

cout << beginning==beginning2;
cout << beginning != beginning2;

真的意味着

(cout << beginning)==beginning2;
(cout << beginning) != beginning2;

尝试     cout&lt;&lt; (开始== beginning2);     cout&lt;&lt; (开始)!= begin2);