472,958 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Overloading operator->

I want to create a const_iterator that gives me access to dynamically
generated objects.
For example see the class Polygon that contains a list of points
stored as following doubles in a vector (x1, y1, x2, y2, ...). The
iterator should return only pairs of doubles to avoid possible
confusion of the user of the class Polygon. (The storage format is
needed for conversion to other libraries.)
The code for Polygon below compiles well, but the problem is using the
operator->.

inline pair<double,doubleoperator->() { return pair<double,
double>(*_points, *(_points+1)); }

If I use the -operator like this:
Polygon p;
p.addPoint(5.0, 5.0);
for(Polygon::iterator it = polygon.begin(); it != polygon.end(); +
+it) {
cout << it->first << endl;
cout << (it).second << endl;
}

I get the following compile error: result of 'operator->()' yields
non-
pointer result
The second line with the * operator gives no errors.

Is it possible to overload the operator-in the intended way?

#include <utility>
#include <vector>
using namespace std;
class Polygon{
public:
class iterator;
inline Polygon() : _points() {};
inline Polygon(unsigned size) : _points(size*2) {};
virtual ~Polygon() {};
inline void addPoint(double x, double y) {
_points.push_back(x);
_points.push_back(y);
}
inline Polygon::iterator begin() const
{ return iterator(*this, 0); }
inline Polygon::iterator end() const
{ return Polygon::iterator(*this, _points.size()); }
inline void clear() { _points.clear(); }
inline void resize(unsigned i) { _points.resize(i*2); }
inline unsigned size() const { return _points.size()/2; }
inline const double* data() const { return &_points[0]; }

class iterator : public
std::iterator<std::random_access_iterator_tag, pair<double, double
{
private:
const Polygon* _polygon;
unsigned _offset;
vector<double>::const_iterator _points;
public:
inline iterator(const Polygon& p, unsigned offset = 0) :
_polygon(&p), _offset(offset), _points(p._points.begin())
{ }
inline iterator(const iterator& i) :
_polygon(i._polygon), _offset(i._offset),
_points(i._points) { }
inline pair<double, doubleoperator*() const
{ return pair<double, double>(*_points, *(_points+1)); }
inline pair<double, doubleoperator[](unsigned i) const
{ return pair<double, double>(*(_points+(i*2)),*(_points+
(i*2)+1)); }

inline pair<double,doubleoperator->()
{
return pair<double,double>(*_points, *(_points+1));
}

inline iterator& operator++()
{ ++_points; ++_points; ++_offset; return *this; }
inline iterator operator++(int)
{ iterator p = *this; ++(*this); return p; }
inline iterator& operator=(iterator& i) {
if (this == &i) { return *this; }
_polygon = i._polygon;
_offset = i._offset;
_points = i._points;
return *this;
}
inline bool operator==(const iterator& i) const
{return _polygon == i._polygon && _offset == i._offset;}
inline bool operator<(const iterator& i) const
{ return _polygon < i._polygon || (_polygon ==i._polygon &&
_offset < i._offset); }
inline bool operator!=(const iterator& i) const { return !
(*this == i); }
inline bool operator>(const iterator& i) const { return i <
*this; }
inline bool operator<=(const iterator& i) const { return !(i <
*this); }
inline bool operator>=(const iterator& i) const { return !
(*this < i); }
};
private:
vector<double_points;
};
#endif

P.S: I use g++ (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21) on
a 64bit x86 AMD machine.

Sep 6 '07 #1
9 2859
Nico wrote:
I want to create a const_iterator that gives me access to dynamically
generated objects.
[..]
Are you going to post it every few minutes? For how long?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 6 '07 #2
On Thu, 06 Sep 2007 10:19:58 -0700, Nico wrote:
>I want to create a const_iterator that gives me access to dynamically
generated objects.
The code for Polygon below compiles well, but the problem is using the
operator->.

inline pair<double,doubleoperator->() { return pair<double,
double>(*_points, *(_points+1)); }

I get the following compile error: result of 'operator->()' yields
non->pointer result
Define an iterator with a pointer as result type.
class iterator : public
std::iterator<std::random_access_iterator_tag, pair<double, double
{
private:

inline pair<double,doubleoperator->()
// pointer and reference are defined in the base class template
pointer operator>() const;
reference operator*() const;

--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Sep 6 '07 #3
Roland Pibinger wrote:
[..]
// pointer and reference are defined in the base class template
pointer operator>() const;
I am guessing you meant

pointer operator->() const;
reference operator*() const;
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 6 '07 #4
On Thu, 6 Sep 2007 17:03:17 -0400, "Victor Bazarov" wrote:
>Roland Pibinger wrote:
>[..]
// pointer and reference are defined in the base class template
pointer operator>() const;

I am guessing you meant

pointer operator->() const;
Yes (the clipboard ate the hyphen).

Sep 6 '07 #5
Define an iterator with a pointer as result type.

I can't return a pointer, because I have no pair-object on the heap!
If I would
return new pair<double,double>(*_points, *(_points+1));
I would create a memory leak!
So I want just to return a copy of my generated pair that will be
deleted when out of scope of the calling function.
>I am guessing you meant
pointer operator->() const;
Of course. But that's not the point.

Sep 7 '07 #6
On 2007-09-07 09:30, Nico wrote:
>Define an iterator with a pointer as result type.

I can't return a pointer, because I have no pair-object on the heap!
If I would
return new pair<double,double>(*_points, *(_points+1));
I would create a memory leak!
So I want just to return a copy of my generated pair that will be
deleted when out of scope of the calling function.
I think returning a smart pointer would work.

--
Erik Wikström
Sep 7 '07 #7
Nico wrote:
>Define an iterator with a pointer as result type.

I can't return a pointer, because I have no pair-object on the heap!
If I would
return new pair<double,double>(*_points, *(_points+1));
I would create a memory leak!
So I want just to return a copy of my generated pair that will be
deleted when out of scope of the calling function.
[snip]

Is there a reason why you have a data member

std::vector<double>

where x-coordinates and y-coordinates are stored alternatingly? If you used

std::vector< std::pair< double, double

instead, the problem would just go away and you could use the iterator type
provided by std::vector.
Alternatively you would have to construct a temporary proxy object that
fakes the pair< double, double reference you want to return. If you want
an iterator and not just a const_iterator, said proxy object would (a)
internally construct a pair<double,doublefrom the values in the vector,
(b) convert silently into a pair<double,double>& on demand, and (c) upon
destruction write back the possibly changed coordinates into the vector
(for this reason, the proxy also needs to remember the indices of the
original values).
Best

Kai-Uwe Bux
Sep 7 '07 #8
On Fri, 07 Sep 2007 09:12:57 -0700, Kai-Uwe Bux wrote:
>Alternatively you would have to construct a temporary proxy object that
fakes the pair< double, double reference you want to return. If you want
an iterator and not just a const_iterator, said proxy object would (a)
internally construct a pair<double,doublefrom the values in the vector,
(b) convert silently into a pair<double,double>& on demand, and (c) upon
destruction write back the possibly changed coordinates into the vector
(for this reason, the proxy also needs to remember the indices of the
original values).
A member in Polygon::iterator would be easier:

// not tested
class Polygon{
class iterator {
pair<double, doublecurrent;
public:
pointer operator->() const {
current = pair<double,double>(*_points, *(_points+1));
return &current;
}
// ...
};
};
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Sep 7 '07 #9
Roland Pibinger wrote:
On Fri, 07 Sep 2007 09:12:57 -0700, Kai-Uwe Bux wrote:
>>Alternatively you would have to construct a temporary proxy object that
fakes the pair< double, double reference you want to return. If you want
an iterator and not just a const_iterator, said proxy object would (a)
internally construct a pair<double,doublefrom the values in the vector,
(b) convert silently into a pair<double,double>& on demand, and (c) upon
destruction write back the possibly changed coordinates into the vector
(for this reason, the proxy also needs to remember the indices of the
original values).

A member in Polygon::iterator would be easier:

// not tested
class Polygon{
class iterator {
pair<double, doublecurrent;
public:
pointer operator->() const {
current = pair<double,double>(*_points, *(_points+1));
return &current;
}
// ...
};
};
That would work for a const_iterator. I do not see, however, how that
mechanism deals with uses of iterator that would need to write to the
container, like

iter->first = 0.2;
Best

Kai-Uwe Bux
Sep 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mohammad | last post by:
I'm implementing a c++ template class CScan to minipulate a series of numbers. I have implemented operator() to select a range of numbers it works fine for an expression like: scan1 = scan2(0,...
3
by: Stub | last post by:
When I try to overload the == operator, it gives me an "error C2804: binary 'operator ==' has too many parameters." bool operator==(const Store& Store1, const Store& Store2); After Adding...
9
by: Silver | last post by:
Hi everyone, I want to overload the ! operator, so that when I write this in main x! I can get the factorial of x. The problem is, that the operator I want to overload takes no parameter. Most...
3
by: md | last post by:
Hi, the following code is working for static objects. ie the statement IntArray x(20); my problem is i want to use this overloading operator for dynamically created objects...
2
by: nayannovellus | last post by:
As we know that both copy constructors and overloading = opeartor copies one object to another then whats the difference between copy constructor and overloading = operator. There must be some...
3
by: Aff | last post by:
hi, i wold like to ask, why is it when overloading operator we send int value is there any way around?. e.g. class abc { protected: int *a; private: int operator(int );
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
11
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my...
0
by: citystud | last post by:
I am trying to convert the c++ code to C#, but find difficulty to convert the overloading operator. Here is the source code. I don't really know how to implement the operator = and operator () in...
1
by: haderika | last post by:
Hey, I'm having trouble overloading the ~ operator (the determinant of the matrix) in a case of 2x2 matrices. So i have a matrix class, with the constructor, overloading +, += and ~ operators. the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.