R
Ross
I have a problem regarding the precedence of overloaded cast operators.
My program is as follows:
#include <stdio.h>
class A;
class B {
public:
B(A &a) {
printf("construct\n");
}
};
class A {
public:
B f() {
printf("f\n");
return(B(*this));
}
operator B () {
printf("convert\n");
return(f());
}
};
int main() {
A a;
a.f();
return(0);
}
When I compile and run the program on Linux using g++, the output is:
f
construct
This is pretty much what I'd expect. However, when I do the same on a
HP-UX system using aCC, the output is:
f
convert
f
convert
f
convert
f
convert
....
This continues until the stack overflows. It seems that the overloaded
cast operator has higher precedence than the contructor of B. I
appreciate this isn't the place to ask about specific C++ compilers, so
my question is: am I making unreasonable assumptions about what the
correct behaviour of C++ should be in these situations?
Thanks in advance,
Ross
My program is as follows:
#include <stdio.h>
class A;
class B {
public:
B(A &a) {
printf("construct\n");
}
};
class A {
public:
B f() {
printf("f\n");
return(B(*this));
}
operator B () {
printf("convert\n");
return(f());
}
};
int main() {
A a;
a.f();
return(0);
}
When I compile and run the program on Linux using g++, the output is:
f
construct
This is pretty much what I'd expect. However, when I do the same on a
HP-UX system using aCC, the output is:
f
convert
f
convert
f
convert
f
convert
....
This continues until the stack overflows. It seems that the overloaded
cast operator has higher precedence than the contructor of B. I
appreciate this isn't the place to ask about specific C++ compilers, so
my question is: am I making unreasonable assumptions about what the
correct behaviour of C++ should be in these situations?
Thanks in advance,
Ross