F
feeblez
Consider the following,
#include <iostream>
using namespace std;
class Foo
{
public:
int i;
Foo() : i(0)
{ cout << "ctor" << endl; }
~Foo()
{ cout << "dtor" << endl; }
Foo(const Foo& rhs) : i(rhs.i)
{ cout << "copy ctor" << endl; }
Foo operator++(int)
{ cout << "postincrement" << endl; Foo temp = *this; ++i;
return temp; }
Foo& operator++()
{ cout << "preincrement" << endl; ++i; return *this; }
Foo& operator=(const Foo& rhs)
{ cout << "assignment" << endl; i = rhs.i; return *this; }
};
int main()
{
Foo f;
f++ = ++f;
}
With sample output:
ctor
preincrement
postincrement
copy ctor
copy ctor
dtor
assignment
dtor
dtor
Can someone explain to me why the preincrement is allowed to be
evaluated first? I thought that this would result in code (in
pseudocode) looking like f.operator++(int).operator=(f.operator++())
-- which would require the postincrement to go first? I mean, there's
a sequence point at the postincrement function call, right?
If I've got this wrong, can someone please explain to me the impact of
operator precedence vs. sequence points in evaluations such as this?
Thanks,
FL
#include <iostream>
using namespace std;
class Foo
{
public:
int i;
Foo() : i(0)
{ cout << "ctor" << endl; }
~Foo()
{ cout << "dtor" << endl; }
Foo(const Foo& rhs) : i(rhs.i)
{ cout << "copy ctor" << endl; }
Foo operator++(int)
{ cout << "postincrement" << endl; Foo temp = *this; ++i;
return temp; }
Foo& operator++()
{ cout << "preincrement" << endl; ++i; return *this; }
Foo& operator=(const Foo& rhs)
{ cout << "assignment" << endl; i = rhs.i; return *this; }
};
int main()
{
Foo f;
f++ = ++f;
}
With sample output:
ctor
preincrement
postincrement
copy ctor
copy ctor
dtor
assignment
dtor
dtor
Can someone explain to me why the preincrement is allowed to be
evaluated first? I thought that this would result in code (in
pseudocode) looking like f.operator++(int).operator=(f.operator++())
-- which would require the postincrement to go first? I mean, there's
a sequence point at the postincrement function call, right?
If I've got this wrong, can someone please explain to me the impact of
operator precedence vs. sequence points in evaluations such as this?
Thanks,
FL