Hello all,
I have a strange compiler behaviour with this code:
---- Begin of code snippet ----
class Base
{
public:
static unsigned int ClassId();
};
class Derived
{
public:
static unsigned int ClassId() { return 2; };
};
class Test
{
public:
Test(unsigned int p1) {};
};
int main(int argc, char * argv[])
{
// This works
Test a(Derived::ClassId());
// This crashes
Test(Derived::ClassId());
return 0;
}
---- End of code snippet ----
The line
Test(Derived::ClassId());
failes to compile with gcc 3.3.5 and Visual Studio 8. With gcc I get
the following error message:
test.cpp: In function `int main(int, char**)':
test.cpp:24: error: prototype for `Test Derived::ClassId()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassId()
test.cpp:24: error: `Test Derived::ClassId()' and `static unsigned int
Derived::ClassId()' cannot be overloaded
test.cpp:24: error: prototype for `Test Derived::ClassId()' does not
match any
in class `Derived'
test.cpp:11: error: candidate is: static unsigned int
Derived::ClassId()
test.cpp:24: error: `Test Derived::ClassId()' and `static unsigned int
Derived::ClassId()' cannot be overloaded
test.cpp:24: error: declaration of `Test Derived::ClassId()' outside of
class
is not definition
So why does it fail with a temporary object in such a way? Should the
compiler not handle this case equivalent to the "non-temporary case"?
Do you have any hints on this behaviour?
Thanks and best regards
Marco