Looking through a lot of documentation it seems they can only return an (int). But i think it would be pretty darned cool if you could write:
Code: Select all
myPointer++;
Code: Select all
myPointer = myPointer->next;
Code: Select all
myPointer++;
Code: Select all
myPointer = myPointer->next;
Code: Select all
unsigned long *x = (unsigned long*)myval;
x++;
Code: Select all
struct MyStruct *x = (struct MyStruct*)myval;
x++;
// C++ Kurs
// Beispiel zu ueberladenen Operatoren
// Klassendefinition
class Rect
{
short xPos, yPos; // Position
short width, height; // Breite und Hoehe
public:
Rect(); // ctors
Rect(short x, short y, short w, short h);
Rect operator ++(int); // Verschiebt Rechteck um eine X/Y-Position
};
// Definition der Memberfunktionen
// 1. Konstruktor (Standard Konstruktor)
Rect::Rect()
{
xPos = yPos = width = height = 0;
}
// 2. Konstruktor
// Erhaelt als Parameter die Rechteck-Daten als 4 short Werte
Rect::Rect(short x, short y, short w, short h)
{
xPos = x; yPos = y;
width = w; height = h;
}
// Verschiebt Rechteck um eine X/Y-Position
// Achtung Postfixoperator!
// Darf keine const-Memberfunktion sein da das Objekt
// ja veraendert wird!
Rect Rect::operator ++(int)
{
// Hilfsobjekt zur Aufnahme der akt. Werte
// Ruft copy-ctor auf
Rect orig(*this);
// Nun erst Position veraendern
xPos++;
yPos++;
// Ursprungswerte zurueckgeben
return orig;
}
Yea, I was meaning this. I was thinking that would be an interesting way to handle list traversal. Something like this:Are you actually meaning that you want the same thing to work for a linked list? If so, I don't see why it wouldn't be possible - but I don't know how advisable this would be.
Code: Select all
myClass *curr;
while(curr != something; curr++)
{
// do stuff
}
I'm assuming this returns the deep copy of xPos & yPos, but incremented by the postfix increment operator.Rect Rect::operator ++(int)
{
// Hilfsobjekt zur Aufnahme der akt. Werte
// Ruft copy-ctor auf
Rect orig(*this);
// Nun erst Position veraendern
xPos++;
yPos++;
// Ursprungswerte zurueckgeben
return orig;
}
Code: Select all
node operator++(int)
{
node* curr = curr->next;
return curr;
}
Code: Select all
void someFuncion()
{
...
curr++;
...
}
curr is null when you dereference it for next. It's an at that point uninitialized local variable.eboyd wrote:This compiles:But crashes when I use it:Code: Select all
node operator++(int) { node* curr = curr->next; return curr; }
Code: Select all
void someFuncion() { ... curr++; ... }
Code: Select all
#include <list>
#include <iostream>
using std::cout;
using std::endl;
using std::list;
typedef std::list<int>::iterator iterator;
int main() {
list<int> numberlist;
numberlist.push_back(1);
numberlist.push_back(2);
numberlist.push_back(3);
for (iterator i = numberlist.begin(); i != numberlist.end(); ++i) {
cout << *i << endl;
}
}
Code: Select all
#include <iostream>
using std::cout;
using std::endl;
int main() {
int numberlist[3] = { 1, 2, 3 };
for (int * i = numberlist; i != numberlist + 3; ++i) {
cout << *i << endl;
}
}
Code: Select all
#include <iostream>
class Foo
{
public:
Foo() : mNext( 0 ) {};
Foo( Foo * foo ) : mNext( foo ) {};
Foo * operator++( int ) { return mNext; }
private:
Foo * mNext;
};
int main()
{
Foo x;
Foo y( & x );
std::cout << &x << std::endl;
std::cout << y++ << std::endl;
return 0;
}
Code: Select all
template <typename T> class dlist
{
struct type
{
...
T data;
type *next, *prev
...
type* operator++( int ) // for postfix incrementer
};
...
};
Code: Select all
template < typename T > class dlist
{
public:
dlist() : head( new type( T() ) ), tail( new type( T() ) ) {}
dlist( const dlist& src ) : head( new type( T() ) ), tail( new type( T() ) )
{
clear();
for( type* temp = ( src.head )->next; temp != src.tail; temp = temp->next )
{
pushb( temp->data ) ;
}
}
~dlist()
{
clear();
delete head;
delete tail;
}
/* I've omitted all my member functions to save space here. */
private:
struct type
{
type( const T& x, type* y = 0, type* z = 0 ) : data( x ), prev( y ), next( z ) {}
type( const type& src ) : data( src.data ), prev( src.prev ), next( src.next ) {}
type* operator++( int )
{
return next;
}
T data;
type *prev, *next;
};
type *head, *tail;
};
Code: Select all
/* This simply reverses the order of the list.
*/
template <typename T> void dlist<T>::rev()
{
if(!empty())
{
type* curr = head->next;
int sz = size();
while(curr != tail)
{
T temp = curr->data;
pushf(temp);
curr++; // this is in place of "curr = curr->next"
}
for(int i = 0; i < sz; i++)
{
popb(); // trim the left-over fat
}
}
}
Yes, that works. That's how I normally would handle traversal.Does it work when you replace curr++ with curr = curr->next?
Code: Select all
curr++;
Code: Select all
curr = curr->next;
Yes. But, only if curr is not a pointer or you satisfy other special conditions that a list cannot.eboyd wrote:Yes, that works. That's how I normally would handle traversal.Does it work when you replace curr++ with curr = curr->next?
But what I'm trying to figure out is ifcan do the dame thing asCode: Select all
curr++;
Code: Select all
curr = curr->next;