cout behaves weirdly - prints elements last-to-first

As an assignment for my beginner C++ course I'm writing a stack that holds strings (char tables). It has an overloaded "operator const char*()" that should enable me to use it's pop method in couts. Most of the time it seems to work nicely, but if I try to pop more than one element in a single line, like this: cout<<s.pop()<<endl<<s.pop<<endl;, it prints the values in reverse order. If I use two couts in two lines, it works well.

For example the code:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	stack s;
	s.push("1");
	s.push("2");
	cout<<s.pop()<<endl<<s.pop()<<endl<<endl;
	s.push("1");
	s.push("2");
	cout<<s.pop()<<endl;
	cout<<s.pop()<<endl;
	return 0;
}


will result in the following output:
1
2

2
1


Here's the source, sorry for the Polish labels, I'll include a little dictionary in the comments ;P

stack.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

class element
{
	public:
		element() : next ( 0 ), tab ( 0 ) {};
		element ( element& );
		~element();
		element ( const char*, element* );
		element ( const char* );
		const char* dajtablice()
		{
			return tab;
		}
		element& operator= ( const element& );
		void wpisz();  //unimportant
		friend class stos;
		operator const char*();
	private:
		element* next;
		char* tab;
};


class stos //"stack"
{
	public:
		stos() :pierwszy ( 0 ){};
		stos ( const stos& );
		~stos();
		void dodaj ( const char* ); //"push"
		element zdejmij(); // "pop"
		element& pokaz(); //unimportant
	private:
		element* pierwszy; //"first"
};


stack.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "stos.h"
#include <cstring>
using namespace std;
const int MAX=100;

void element::wpisz() //unimportant
{
	char c[MAX];
	cout<<"Napisz se coś"<<endl;
	cin>>c;
	tab=new char[strlen ( c ) +1];
	strcpy ( tab,c );
};

element::~element()
{
	if ( tab )
		delete[] tab;
}

element::element ( element& e )
{
	if ( e.tab )
	{
		next=0;
		tab=new char[strlen ( e.tab ) +1];
		strcpy ( tab, e.tab );
	}
}

element& element::operator= ( const element& r )
{
	if ( !r.tab )
	{
		if ( tab )
			delete[] tab;
		tab=0;
		return *this;
	}
	else if ( tab )
	{
		if ( strlen ( tab ) <strlen ( r.tab ) )
		{
			delete[] tab;
			tab=new char[strlen ( r.tab ) +1];
		}
	}
	else
		tab=new char[strlen ( r.tab ) +1];
	strcpy ( tab, r.tab );
	return *this;
}

void stos::dodaj ( const char* c ) //"push"
{
	if ( strlen ( c ) )
	{
		element* drugi=pierwszy;
		pierwszy=new element ( c, drugi );
	}
}

element::element ( const char* c, element* e )
{
	tab=new char[strlen ( c ) +1];
	next=e;
	strcpy ( tab,c );
}

element stos::zdejmij() //"pop"
{
	element ret ( *pierwszy );
	element* temp=pierwszy;
	pierwszy=pierwszy->next;
	delete temp;
	return ret;
}
element& stos::pokaz()
{
	return *pierwszy;
}

element::operator const char*()
{
	return tab;
}

element::element ( const char* c )
{
	next=0;
	tab=new char[strlen ( c ) +1];
	strcpy ( tab,c );
}

stos::~stos()
{
	element* temp;
	while ( pierwszy )
	{
		temp=pierwszy;
		pierwszy=pierwszy->next;
		delete temp;
	}
}


example main.cpp to demonstrate problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "stos.h"

using namespace std;

int main()
{
	stos s;
	s.dodaj("1");
	s.dodaj("2");
	cout<<s.zdejmij()<<endl<<s.zdejmij()<<endl<<endl;
	s.dodaj("1");
	s.dodaj("2");
	cout<<s.zdejmij()<<endl;
	cout<<s.zdejmij()<<endl;
	return 0;
}


The output, as I said earlier, is:
1
2

2
1


I'd like to know what the hell does this ;) and how can I fix it.
thanks in advance for the help.
It is because of sequence points (go look it up on wikipedia if you don't know what that is).

The order of function calls on line 10 is undefined, for VC++, it seems to go right to left, so it will call the right zdejmij() first, giving it 2, then call the first one, giving it the value 1. However, like I said, it is undefined, so the program could do it in any order it feels like.
The << doesn't guarantee that the operands are evaluated from left to right so the second pop may be called before the first one and as a result you'll get the values to be printed in reverse order
Thanks for the quick reply ;)
Topic archived. No new replies allowed.