簡體   English   中英

重載運算符的返回值

[英]return value for overloaded operator

如果這是一個非常基本的問題,我很抱歉,我對C ++很陌生。 我正在嘗試為它定義自己的向量類和迭代器。 但是,每當我重載操作符時,返回的值始終是一個地址。

例如,當我想要打印1 0時,以下代碼打印0x7fb6dbc000e0 0x7fb6dbc000e0

由於我一直在搞亂語法一段時間,一些運算符看起來有點不同,這只是為了讓你看到我嘗試過的一些東西。

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];
}
};
cout << beginning==beginning2;

並不意味着

cout << (beginning==beginning2);

意味着

(cout << beginning) == beginning2;

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

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

<<運算符的優先級高於==和!=運算符的優先級 所以

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

真正意思

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

嘗試cout <<(開始==開始2); cout <<(開始)!=開始2);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM