Reputation: 13
I was told to use proxy pattern in my program which is not that clear to me.
I have some issues with Proxy &operator*()
, I don't know what should I return there to get a value of current index in file.
I had this before and it worked:
int &operator*()
{
return ptr->getValue(index);
}
But I was told to change it to Proxy
.
If I compile with Proxy &operator*()
then I'm getting
error: invalid initialization of reference of type 'IntFile::Proxy&' from expression of type 'int'|
Full code:
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
class IntFile
{
public:
int value;
FILE* file;
IntFile()
{
file = fopen("text.txt", "r+b");
}
~IntFile()
{
fclose(file);
}
virtual int& getValue(int index)
{
fseek(file, 4*index, SEEK_SET);
fread(&value, 4, 1, file);
return value;
}
virtual int setValue(int value)
{
fseek(file, 0, SEEK_CUR);
fwrite(&value, 4, sizeof(value),file);
return 0;
}
class Proxy
{
private:
IntFile *ptr;
long index;
public:
Proxy(IntFile* ptr3)
{
ptr = ptr3;
}
Proxy& operator=(int value)
{
ptr->setValue(value);
}
operator int() const
{
return ptr->getValue(index);
}
};
friend struct iterator;
struct iterator
{
int index;
int value2;
IntFile* ptr;
iterator(IntFile* ptr2, int idx, FILE* ptrfile)
{
ptr = ptr2;
index = idx;
fseek(ptrfile, 4*index, SEEK_SET);
}
bool operator==(const iterator&other) const
{
return index == other.index;
}
bool operator!=(const iterator&other) const
{
return index!=other.index;
}
Proxy &operator*()
{
// How to do that?
}
int &operator=(int value)
{
this->value2 = value;
}
iterator&operator++()
{
this->index = index+1;
}
iterator&operator--()
{
this->index = index -1;
}
};
iterator begin()
{
return iterator(this, 0, file);
}
iterator end(int number)
{
return iterator(this, number, file);
}
iterator rbegin(int number)
{
return iterator(this, number-1, file);
}
iterator rend()
{
return iterator(this, -1, file);
}
};
int main()
{
IntFile myfile;
int number;
cout << "Enter number of elements: " << endl;
cin >> number;
vector <int> myVector;
cout << "Enter your numbers: ";
for ( int i = 0; i < number; i++)
{
cin >> myfile.value;
myVector.push_back(myfile.value);
}
fwrite(&myVector[0], sizeof(vector<int>::value_type), myVector.size(),myfile.file);
cout << endl << "FORWARD 1 by 1: " << endl;
for (IntFile::iterator i = myfile.begin(); i != myfile.end(number); ++i)
{
cout << *i << " ";
}
cout << endl << "BACKWARD 1 by 1: " << endl;
for (IntFile::iterator i = myfile.rbegin(number); i != myfile.rend(); --i)
{
cout << *i << " ";
}
cout << endl;
return 0;
}
Upvotes: 1
Views: 115
Reputation: 302
Try this
Proxy &operator*()
{
// How to do that?
return *this;
}
It avoid you to create another new Proxy
Upvotes: 0
Reputation: 170239
Returning a proxy requires of you to return a proxy object. You attempt to return a reference to such an object, but it doesn't exit. And we cannot bind a proxy reference to an integer.
The fix is simple, just return a new object by value:
Proxy operator*()
{
return Proxy(ptr);
}
Upvotes: 1