Hi there,
I have the following C++ code in Visual Studio 2005...
class Base {};
class Derived : public Base {};
class Other {
public:
Other(const Base& obj) {}
void test() {}
};
int _tmain(int argc, _TCHAR* argv[])
{
Other other(Derived());
other.test();
return 0;
}
... Compilation fails and gives:
test.cpp(19) : error C2228: left of '.test' must have class/struct/union
I've determined through a few tests that this happens because the declaration of the "other" variable is interpreted as a function declaration (returning a Other and taking a Derived parameter), instead of an instance of Other using the single-argument constructor. (VS6 finds the constructor and compiles it fine, but it's not good at standard C++ so I don't trust it compared to VS2005)
If I do...
Other other(static_cast<Base&>(Derived()));
... or use copy-initialization, it works fine. But it doesn't seem to see that the Derived() instance is derived from Base on its own, or it prioritizes function declaration instead of trying for polymorphism on the constructor parameter.
My question is: is this standard C++ behavior, or is this VS2005-specific behavior? Should...
Other other(Derived());
... declare a local instance in standard C++, or should it declare a function?